none
Entity Framework + BaseEntity class

    Question

  • Hi,

    I'm trying to have a BaseEntity class with common properties like ID, creationDate, creationUser, modificationDate and modificationUser. I want to use the Entity Framework, but I don't know how to do it.

    I've completed the database creation and I've created a new Entity Model based on it. I've seen that for Linq to SQL, it is possible to specify a BaseEntity, but only with the line command tool. How do we do with the Entity Framework ? Do I have to manually create a BaseEntity class and inherite all the classes in the generated .cs file from it ? Is it working so ?

    I've a database containing about 15 tables and the generated source file is quite big already. What is the correct way to proceed? Should we create many models with each one representing a smaller part of the database? I guess we could do it if one part of the DB is separated from the other part, but what happens to a table mapped on two different models?


    dvarrin
    Wednesday, April 15, 2009 8:03 AM

All replies

  • Hi-

    There isn't a way to automatically specify a base entity with EdmGen.exe or the Designer (although we are working to make code generation more flexible in the next EF release). However, you can currently use the Designer to create inheritance hierarchies. Here are some resources that might help:

    Walkthrough: Mapping Inheritance - Table-per-Hierarchy

    Walkthrough: Mapping Inheritance - Table-per-Type

    Tip 12 - Choosing an Inheritance Stategy

    Not sure I've exactly answered your question, but hope that helps.

    -Brian
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Wednesday, April 15, 2009 4:28 PM
  • Hi Brian,

    Thank you very much for your answer.

    I was trying to use inheritance, but I don't understand how to do the mapping. I've checked the Walkthrough for the School example with Course, OnlineCourse and OnSiteCourse. In that case, there is a table for the Course Entity, even if it is an abstract entity, because it contains the common properties between OnlineCourse and OnSiteCourse.

    I've seen that there is a Inheritance with Table per Concrete Class, But I don't understand how to do it. If I create a table for a person for example, with Id, firstname and lastname and I create the edmx model with that table, then if I want to inherite the Id from a superclass, I created a "BaseEntity" entity with the Id property and inherite the person entity from it. So I've to remove the Id from the person entity, but on the mapping I cannot define anything for the Id in the table. Shouldn't be able to select the Id of the superclass? I can select the Id only if the superclass has it's own table too.


    Best regards,
    Daniel
    dvarrin
    Thursday, April 16, 2009 7:15 AM
  • Daniel-

    Unfortunately, the Designer does not support TPC mapping. For that, you will have to manually edit the .edmx file. There are hints of how to do this in the blog post I linked to in my first post. This mapping helper might be helpful too: Entity Framework Mapping Helper.

    -Brian

     

     


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Thursday, April 16, 2009 5:10 PM
  • Thank you very much for your help Brian. I'll try the Mapping Helper if I still think I need that TPC mapping.

    Daniel
    dvarrin
    Monday, April 20, 2009 7:06 AM
  • This is my first time to use EF and I ran into this problem right the way. I have spent quite a few hours on this. After manually editing the mapping file and made it work, with one BaseEntitySet in the context. So, I have to do context.BaseEntitySet.OfType<Person>() to query the right table because all tables/classes are sharing the same BaseEntitySet. But it always gives me non-unique ID warnings because same ID  exists in different tables.

    I have seen another thread describing how to manually editing the .edmx file to create one Entity Set per table. I followed the steps and it didn't work for me. Even I can make it work after so much twisting, I don't feel it's a maintainable solution.


    Anyway, my questions are,
    1. Is there a really solution for version 1?
    2. Will it be supported in the new version?
    3. When will the new version of EF be released?

    I just can't believe such a popular use case is not supported. Would somebody please give me answers ASAP? I have to make a decision to pick a OR mapping tool really quick.

    Thanks in advance!

    Richard.

    Tuesday, August 04, 2009 9:06 PM
  • By the way, here is the thread I followed to make it work. But there are a few compiler warnings making me feel uncertain.

    http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/f0129e5d-790a-4519-bf94-0f56a6a6d345
    Wednesday, August 05, 2009 3:24 AM