Creating groups with EF 5 RRS feed

  • Question

  • Hi

    I am working with EF5 code first and wanted to introduce groups in to my database structure.

    For example Product Groups, Country Groups, Customer Groups and User Groups.

    A user is a sales person and they may deal with a group or various groups of products. Likewise they may deal with a group or groups of countries and customers.

    The idea being that this is flexible and configurable so that for example a sales person could deal with a product across all countries and customer groups or it could be restricted by adding in a particular country or customer group and any combination of the above.

    To make it easier to assign things a sales person is always part of a user group and the above relationships are defined not against an individual, but against a user group. This way I can quickly assign new users to a group and they will inherit the correct products, customer, countries etc etc.

    Another point is that a user can be a member of multiple user groups.

    What I am struggling with is how I create this structure in code first?

    Does each of Product, Country, Customer and User need a separate Group table and then another table called "ProductGroup", "UserGroup" etc.

    Or should I have 1 Group table that has a field that defines the Group Type (e.g. User, Product, Country) and then Separate "ProductGroup", "UserGroup" tables or again can this be combined in to perhaps 2 tables separating "UserGroups" from "PermissionGroups".

    So "PermissionGroups" has:

    PermissionType (Product, Country, Customer)
    GroupID (Being the Id of Group within the selected Type)

    There would then be a separate table called "UserGroupPermissions" that references the UserGroup and the PermissionGroup, i.e.


    This probably seems straight forward to most people but I am struggling with how to define this in code first and what tables I would need. Also in the example of the PermissionsGroup table for example where you select a "PermissionType" how would this work from a selection perspective.

    What I mean is usually you may include a public ProductGroup ProductGroup and a public int ProductGroupId, but as this can be a product, country or customer group how would I deal with this?

    I hope you can help,


    Friday, March 8, 2013 9:41 AM


  • If you forget about what the tables should look like, how do you think it should look in code? What C# classes do you want to use?

    I think you could probably get most of the mappings you suggested here to work, so if you define what you want it to look like in code then we can look at a good way to map it to the database.

    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    Saturday, March 9, 2013 1:55 AM