Deriving all Entitiers from a single abstract base RRS feed

  • Question

  • I already have a larger working project (150+ tables) using a different ORM tool, and before I go off and spin my wheels going down the wrong path with EF (4.4 - .Net 4.0 platform) I want to know if the design I currently use will work the same way in EF.

    Currently I have all my Entities (Table  per type) inherit form a single abstract base type (BaseEntity) that contains 2 properties, pk and rowversion. 

    Several questions I have from reading various postings on the forums


    1. You cannot have an un-mapped entity??  So this means I would have to have a table for the base?  And thsat the primary key pk would have to be unique across all types?  My current tool does not have a problem with this. Is this true or is there a way around it?

       part a) If an Interface is the only way, then can I get EF to make all my entities use that interface, again without all the tedious typing ? 

    2. Since every table contains its on pk, rowversion column, the Model Designer, when creating it from my existing database loads all the columns into every table, and then I have to remove them all, and than I have to create my abstract base entity, and all the associations and then the mappings.  This seems like a lot of busy work, can I justr tell EF to make all my entity be subclass of BaseEntity and somehow not have to do all the pk,rowversion removals since BaseEntity already has them and they are marked virtual?




    • Edited by James Klett Thursday, December 27, 2012 9:38 PM
    Thursday, December 27, 2012 9:38 PM

All replies

  • Hi James,

    I am not familiar with this issue, but I will help to bomb this issue up.

    Good luck.

    Call me ghost for short, Thanks
    To get the better answer, it should be a better question.

    Friday, December 28, 2012 9:32 AM
  • is "bomb this up" a good thing or a bad thing?  I must be getting old, don't know your people slang


    Friday, December 28, 2012 5:40 PM
  • When more responses involved, it should be good things, or nothing bad, nothing good :)

    Call me ghost for short, Thanks
    To get the better answer, it should be a better question.

    Monday, December 31, 2012 1:38 AM
  • Hi,

    If the database has created, and you want to create the tables by the database, the solution couldn't work. As you have researched, EF doesnt' support un-mapped entity.

    If you want to use EF, and all of your tables in the database have already contain own pk and rowversion, there're two ways to do.

    1. Delete all the pks and rowversions from the tables, also map the base table to EF model.

    2. Delete the base table. But this seems go against your design.

    Monday, December 31, 2012 1:42 AM
  • With an existing database and gig's of data I cannot just delete the PK from the tables.  That Solution is a non solution,



    Wednesday, January 2, 2013 4:54 PM
  • Hi James,

    You can do what you are after, but you will need to use Code First instead of the designer.

    If you already have POCOs for your domain then you can create the mapping code for each class and reuse the existing objects. This will probably take a while as well.

    The alternative is probably to reverse engineer a code first model using the power tools, that will create you classes for each of your tables and you can then extract the abstract base class and fix up the inheritance. You shouldn't have to change the mapping in this scenario I don't think.

    There will almost certainly be work in getting an EF domain to match your existing database structure, but I haven't seen anything in your description that code first can't do. There is more information on how to use code first on the EF getting started page if you haven't seen it: http://msdn.microsoft.com/en-us/data/ee712907

    Let me know if you have any trouble that need help with.

    EDIT: I am also assuming that you are using EF 5.0 with .NET 4.0. If so then your assembly info will say 4.4 but it is actually EF 5.

    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.

    Tuesday, January 8, 2013 10:50 PM
  • Hi, James

    Interface looks most painless way. EF in database first and model first modes allows you to generate entities using T4 templates (just right-click designer surface -> Add code generation item) that can be modified to add this interface to them all.

    Friday, January 11, 2013 11:08 AM