none
Interface style Inheritance RRS feed

  • Question

  • I would like to abstract out a base class (Interface may be a better term) for all of my tables in my database.  Basically I have a few common column names across all tables in the database (Primary Key and Audit Trail information).  I would like to use a base class (call it BaseObject) which contains all of these fields and which is inherited by all other Enitites in the model.  If I take the Database first approach and create the "default" EDMX model and manually create the BaseObject and remap my Entities to use inheritence from this BaseObject in the Conceptual Model all starts to look good.  The problem that I'm seeing when I go to the Mapping Details screen to map the Schema Model, the Primary Key field is not an option that can be picked from the Value/Property dropdown as it's in the BaseObject Entity.  Is it possible to map this PK some other way that I'm missing?  Without the PK mapped, how will EF be able to interact with all of the underlying Tables?

    Monday, December 5, 2011 9:13 PM

Answers

All replies

  • Hi Christopher,

    Welcome!

    I think it is impossible to map Pk in your scenario, thanks for understanding.

    Here some mapping scenarios in EF: http://msdn.microsoft.com/en-us/library/cc716779.aspx

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, December 6, 2011 9:03 AM
    Moderator
  • I was reviewing the link you provided, which pointed me towards "Mapping Inheritance - Table-per-Type (Entity Data Model Tools)" (http://msdn.microsoft.com/en-us/library/cc716702.aspx) which looked like it may have some promise, but the instructions sound a little off.  When following through these, in #2 you delete the Key field and then in #5 you map the field you use deleted???  So if I have a field name ModifiedDate on my "BaseObject" that I want inherited by other Entities, when following these steps I delete "ModifiedDate" from the entity "Foo", it no longer there to remap in step #5 unless I'm missing something. 

    1. Delete the association between the OnlineCourse entity type and the Course entity type. Delete the association between the OnsiteCourse entity type and the Course entity type. These associations will be replaced by implementing the inheritance hierarchy.

    2. Delete the CourseID key property from the OnlineCourse and OnsiteCourse entity types. This property will be inherited from the Course entity type.

    3. Set the Course entity type as the base type for the OnlineCourse and OnsiteCourse entity types.

    4. Make the Course entity type an abstract type.

    5. Map the inherited CourseID properties of the OnlineCourse and OnsiteCourse entity types to the appropriate columns.

     

     

    Tuesday, December 6, 2011 3:08 PM
  • Hi Christopher,

    When we delete the Key field from the Designer, it means we just remove it from CSDL(no changes in SSDL)and the mapping means map the CSDL(Base class) to SSDL. TPT inheritance is one to one ralationship.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, December 13, 2011 8:50 AM
    Moderator
  • Hi,

    I am writing to check the status of the issue on your side.  Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, December 19, 2011 3:42 AM
    Moderator
  • I believe the approach that matches more inline with what i'm attempting to achieve is via the Table per Concrete Type (TPC) in EF 4.2 which is apparently not supported in the current VS 2010 EF Modeling tool, but is supported via the fluent api in the updated EF if you already have POCO objects.

    http://weblogs.asp.net/manavi/archive/2011/01/03/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-3-table-per-concrete-type-tpc-and-choosing-strategy-guidelines.aspx
    
    Thursday, January 5, 2012 9:52 PM