locked
Table-per-Type, store typecode in abstract entity RRS feed

  • Question

  • Hi there,

     

    I'm trying to use Table-per-type fuctionality of EntityFramework. I have 3 tables in SQL: BaseEntity, Contact, and Company. I've created BaseEntity as the abstract entity class and two entities Contact and Company tha derive from BaseEntity.

     

    The problem I'm having is with Entity_TypeCode (int) column inside BaseEntity table. It's not being populated at the moment because it's not mapped to anything. Basically, when a user adds a record, e.g. Contact or Company, I want that column to be populated with value 1 for Contact and 2 for Company.

     

    Note that the Entity_TypeCode column doesn't exist in Contact or Company tables, only in BaseEntity. That's where it needs to be populated.

     

    Is this possible? If yes, could somebody please point me to an example?

     

    The reason I want to store the typecode additionally is because at some point I want to have a Permissions table in SQL that will list all access rights for users to different objects. E.g. a user might have Read/Write/etc access to both Contact and Company entries, and I'd rather have one table to store such information rather than having separate tables, e.g. UserCompanyPermissions, UserContactPermissions, etc.

     

    Thanks,

    -Dash.

    Saturday, November 1, 2008 6:11 PM

Answers

  • I was able to accomplish this by setting the value of TypeCode inside the class' default constructor and also making the setter of the property protected.

     

    Code Snippet
        partial class Company
        {
            public Company()
            {
                Entity_TypeCode = 2;
            }
        }

     

     

     

    Thanks,

    -Dash.

    Saturday, November 1, 2008 10:41 PM

All replies

  •  

    Hi DashNY,

     

    It looks like you are trying to use a discriminator column although with having different tables, you ought to be use Table Per Type. I dont think it is possible the way you are trying to accomplish this.

     

    The way i think this might work would be, you create your inheritance hiearachy using discriminator column.

    and then each derived type can use table per model to model addtiional columns. from Contact and Company.

    so in short, if you start with Table Per Hiearachy and then lend the model to Table Per Type, then i think this could be accomplished.

     

    Zeeshan hirani

     

    Saturday, November 1, 2008 8:53 PM
  • I was able to accomplish this by setting the value of TypeCode inside the class' default constructor and also making the setter of the property protected.

     

    Code Snippet
        partial class Company
        {
            public Company()
            {
                Entity_TypeCode = 2;
            }
        }

     

     

     

    Thanks,

    -Dash.

    Saturday, November 1, 2008 10:41 PM