locked
Entity Framework Code First to Existing Database Error 6002 RRS feed

  • Question

  • Hello all,

    I have applied Code First to an Existing Database (following this tutorial : http://msdn.microsoft.com/en-us/data/jj200620.aspx).

    It created the context and all classes correctly. But it gave this message for every table in the database :

    Warning 20 Error 6002: The table/view 'xx' does not have a primary key defined. The key has been inferred and the definition was created as a read-only table/view.

    All tables have an Id which has Id mark checked. What should I do for this? Should I ignore this message or do something else?

    Thanks.

    • Moved by CoolDadTx Wednesday, December 10, 2014 4:57 PM EF related
    Wednesday, December 10, 2014 1:57 PM

Answers

  • You need to decorate identity columns with the Key attribute, eg:

    public partial class Blog 
    { 
        public Blog() 
        { 
            Posts = new HashSet<Post>(); 
        } 
    
        [Key] 
        public int BlogId { get; set; } 
     
        [StringLength(200)] 
        public string Name { get; set; } 
     
        [StringLength(200)] 
        public string Url { get; set; } 
     
        public virtual ICollection<Post> Posts { get; set; } 
    }


    Hope that helps
    Please don't forget to upvote posts which you like and mark those which answer your question.

    • Marked as answer by Fred Bao Monday, December 22, 2014 9:23 AM
    Wednesday, December 10, 2014 3:23 PM
  • I have realized that [Key] was put in many property in classes.

    So I guess I should remove [Key] from those which are not identities, right?

    No not necessarily becuase there can be a primary-key that it not based on Idenity auto incremented column. The primay-key can be programmically generated and applied as the primary-key, like a GUID being used as the primary-key or other means to generate the primary-key other than using an Idenity int auto increamented column as the primary-key.

    http://peterkellner.net/2012/01/15/entityframework-codefirst-and-autokey-generation/

    • Marked as answer by Fred Bao Monday, December 22, 2014 9:24 AM
    Wednesday, December 10, 2014 4:04 PM

All replies

  • You need to decorate identity columns with the Key attribute, eg:

    public partial class Blog 
    { 
        public Blog() 
        { 
            Posts = new HashSet<Post>(); 
        } 
    
        [Key] 
        public int BlogId { get; set; } 
     
        [StringLength(200)] 
        public string Name { get; set; } 
     
        [StringLength(200)] 
        public string Url { get; set; } 
     
        public virtual ICollection<Post> Posts { get; set; } 
    }


    Hope that helps
    Please don't forget to upvote posts which you like and mark those which answer your question.

    • Marked as answer by Fred Bao Monday, December 22, 2014 9:23 AM
    Wednesday, December 10, 2014 3:23 PM
  • Thank you for your response Mr. Oneill.

    I have realized that [Key] was put in many property in classes.

    So I guess I should remove [Key] from those which are not identities, right?

    Thank you.

    Wednesday, December 10, 2014 3:33 PM
  • Definitely.

    If you put it on all the fields then the whole lot would become a weird composite key.


    Hope that helps
    Please don't forget to upvote posts which you like and mark those which answer your question.

    Wednesday, December 10, 2014 3:47 PM
  • I have realized that [Key] was put in many property in classes.

    So I guess I should remove [Key] from those which are not identities, right?

    No not necessarily becuase there can be a primary-key that it not based on Idenity auto incremented column. The primay-key can be programmically generated and applied as the primary-key, like a GUID being used as the primary-key or other means to generate the primary-key other than using an Idenity int auto increamented column as the primary-key.

    http://peterkellner.net/2012/01/15/entityframework-codefirst-and-autokey-generation/

    • Marked as answer by Fred Bao Monday, December 22, 2014 9:24 AM
    Wednesday, December 10, 2014 4:04 PM
  • Errm.....

    The script for the tables is in the link above:

    CREATE TABLE [dbo].[Blogs] ( 
        [BlogId] INT IDENTITY (1, 1) NOT NULL, 
        [Name] NVARCHAR (200) NULL, 
        [Url]  NVARCHAR (200) NULL, 
        CONSTRAINT [PK_dbo.Blogs] PRIMARY KEY CLUSTERED ([BlogId] ASC) 
    ); 
     
    CREATE TABLE [dbo].[Posts] ( 
        [PostId] INT IDENTITY (1, 1) NOT NULL, 
        [Title] NVARCHAR (200) NULL, 
        [Content] NTEXT NULL, 
        [BlogId] INT NOT NULL, 
        CONSTRAINT [PK_dbo.Posts] PRIMARY KEY CLUSTERED ([PostId] ASC), 
        CONSTRAINT [FK_dbo.Posts_dbo.Blogs_BlogId] FOREIGN KEY ([BlogId]) REFERENCES [dbo].[Blogs] ([BlogId]) ON DELETE CASCADE 
    );

    BlogId and PostId should be marked  [Key]


    Hope that helps
    Please don't forget to upvote posts which you like and mark those which answer your question.

    Wednesday, December 10, 2014 4:25 PM