none
Code First Data Annotations Generation bug report RRS feed

  • Question

  • When you generate a code first model:

    When the table has a primary key called "id" or "Id", it should generate a "Key" Data Annotation as shown in: https://msdn.microsoft.com/en-us/data/gg193958.aspx , which is actually not doing it.

    Could you please look at this bug? I need these attribute to get the PK's of whichever table is added to my project.

    Thanks,

    Joshua D. Zatarain E.

    Tuesday, April 12, 2016 3:56 PM

Answers

  • Hi jzrobot,

    According to your description, you use code first approach, it seems that you generate model from the database, and I create a demo, as you said, if the table has a primary key called "Id" OR "xxxId", it doesn't generate a "Key" Data Annotation, from the article you provided, we know that it is a convention, which is to look for a property named “Id” or one that combines the class name and “Id”, such as “BlogId”. In addition to EF’s use of the EntityKey, the property will map to a primary key column in the database.  

    >>Could you please look at this bug? I need these attribute to get the PK's of whichever table is added to my project.

    You could add to "Key" Data Annotation to the property named "Id" by manually.

    Best regards,

    Cole Wu

    Wednesday, April 13, 2016 2:20 AM
    Moderator
  • I believe what cole wu means is like

    public class Blog 
    { 
        [Key] 
        public int PrimaryTrackingKey { get; set; } 
        public string Title { get; set; } 
        public string BloggerName { get; set;} 
        public virtual ICollection<Post> Posts { get; set; } 
    }

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

    chanmm


    chanmm

    Tuesday, April 19, 2016 8:41 AM

All replies

  • Hi jzrobot,

    According to your description, you use code first approach, it seems that you generate model from the database, and I create a demo, as you said, if the table has a primary key called "Id" OR "xxxId", it doesn't generate a "Key" Data Annotation, from the article you provided, we know that it is a convention, which is to look for a property named “Id” or one that combines the class name and “Id”, such as “BlogId”. In addition to EF’s use of the EntityKey, the property will map to a primary key column in the database.  

    >>Could you please look at this bug? I need these attribute to get the PK's of whichever table is added to my project.

    You could add to "Key" Data Annotation to the property named "Id" by manually.

    Best regards,

    Cole Wu

    Wednesday, April 13, 2016 2:20 AM
    Moderator
  • I believe what cole wu means is like

    public class Blog 
    { 
        [Key] 
        public int PrimaryTrackingKey { get; set; } 
        public string Title { get; set; } 
        public string BloggerName { get; set;} 
        public virtual ICollection<Post> Posts { get; set; } 
    }

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

    chanmm


    chanmm

    Tuesday, April 19, 2016 8:41 AM