none
Inherited Primary Key (Entity Key) Not Recognized by EF RRS feed

  • Question

  • Hi friends

    I am using EF's Code First approach and TPC inheritance.

    I have an abstract base class ("Ad") which defines a primary key named "AdID" which is also the identity. Two concrete classes ("SellingAd" & "BuyingAd") inherit from this base class. I want these derived classes to have the same property ("AdID") as PK and identity.

    My problem is that

    1. In Visual Studio, while typing, Intellisense shows "AdID" in the list of properties of derived classes, and
    2. EF creates a column in DB in both tables mapped to "SellingAd" & "BuyingAd", but
    3. When I try to insert some records to DB I get the following error

    The property 'AdID' is not a declared property on type 'SellingAd'. Verify that the property has not been explicitly excluded from the model by using the Ignore method or NotMappedAttribute data annotation. Make sure that it is a valid primitive property.

    What might the cause be and the solution?
    Many thanks in advance


    There are no closed doors, only unopened ones.


    • Edited by Kamran Saeedi Friday, February 17, 2012 10:18 PM typing error
    Wednesday, February 15, 2012 8:45 AM

All replies

  • Hi Kamran Saeedi,

    Welcome to MSDN Forum.

    Could you please post the code that how do you realize TPC? This is, so I can help you more effectively.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Friday, February 17, 2012 2:26 AM
    Moderator
  • By the naming convention the primary key property must be called "Id" or "<calss_name>Id" and it's case-sensitive restriction. Try to call the key as "Id"

    Thanks


    • Edited by eugene_s Friday, February 17, 2012 1:53 PM
    Friday, February 17, 2012 11:08 AM
  • By the naming convention the primary key property must be called "Id" or "<calss_name>Id" and it's case-sensitive restriction. Try to call the key as "Id"

    Thanks


    eugene_s

    Thanks for your response, but what you said is not correct as per below: (http://blogs.msdn.com/b/efdesign/archive/2010/06/01/conventions-for-code-first.aspx)

    Primary Key

    Previously Code First would infer that a property is a primary key if the property is called ‘Id’ or ‘<class name>Id’. The only change to this convention is that once primary key properties are detected if their type is ‘int’, ‘long’ or ‘short’, they are registered as identity columns in the database by default. Primary key detection is not case sensitive.


    There are no closed doors, only unopened ones.


    Friday, February 17, 2012 9:08 PM
  • Dear Allen

    The code is posted at http://stackoverflow.com/questions/9295901/inherited-primary-key-not-recognized-by-ef

    Thanks for your time


    There are no closed doors, only unopened ones.

    Friday, February 17, 2012 9:10 PM
  • Hi Kamran,

    Based on the code, you didn't create DBSet for the three class in the 'MyContainer'. I have tested the code and modify the BuyingAdConfiguration and SellingAdConfiguration. You set the AdId for AD class as 'DatabaseGeneratedOption.Identity', but the other derived classes as 'DatabaseGeneratedOption.None', Entity Framework will throw an exception about conflict. I modified the two derived classes's AdId property also as 'DatabaseGeneratedOption.Identity', and then try to insert a instance to create the database. The database can be created and below is the tables in the database.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Monday, February 20, 2012 8:39 AM
    Moderator