locked
EF Code First from Database does not add Key attribute if property name ends with ID (*ID) RRS feed

Answers

  • Hi Ashwini47,

    >>All my primary keys in DB are like TableName+Id but when I generated Model from DB it does`t worked 

    Is it that it does not add key attribute? It’s
    not a known issue, it’s a convention, the convention is to look for a property
    named “Id” or one that combines the class name and “Id”, such as “BlogId”. The
    property will map to a primary key column in the database.

    Best regards,

    Cole wu


    Tuesday, January 19, 2016 6:23 AM

All replies

  • Hi Ashwini47,

    There are three different ways to create primary key using Code First:

    Convention: Property with name "Id" or property with name {class name} + "Id" (both are case insensitive)
    Data Annotation: [Key] attribute
    Fluent API: Entity<T>.HasKey(p => p.PropertyName) function

    You could refer to the following links for more information.

    https://msdn.microsoft.com/en-us/data/jj591583.aspx

    http://www.codeproject.com/Articles/813912/Create-Primary-Key-using-Entity-Framework-Code-Fir

    Best regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, January 15, 2016 8:37 AM
  • Hi Fred 

    All my primary keys in DB are like TableName+Id but when I generated Model from DB it does`t worked 

    Is this a known issues or product bug 

    Regards

    Ashwini

    Monday, January 18, 2016 4:39 AM
  • Hi Ashwini47,

    >>All my primary keys in DB are like TableName+Id but when I generated Model from DB it does`t worked 

    Is it that it does not add key attribute? It’s
    not a known issue, it’s a convention, the convention is to look for a property
    named “Id” or one that combines the class name and “Id”, such as “BlogId”. The
    property will map to a primary key column in the database.

    Best regards,

    Cole wu


    Tuesday, January 19, 2016 6:23 AM