Error: the entity type requires a primary key

后端 未结 11 1593
走了就别回头了
走了就别回头了 2020-12-02 15:30

I would like to expand the question asked on this thread

Binding listbox to observablecollection

by giving it an ability to persistent the data. The structur

11条回答
  •  温柔的废话
    2020-12-02 15:56

    I came here with similar error:

    System.InvalidOperationException: 'The entity type 'MyType' requires a primary key to be defined.'

    After reading answer by hvd, realized I had simply forgotten to make my key property 'public'. This..

    namespace MyApp.Models.Schedule
    {
        public class MyType
        {
            [Key]
            int Id { get; set; }
    
            // ...
    

    Should be this..

    namespace MyApp.Models.Schedule
    {
        public class MyType
        {
            [Key]
            public int Id { get; set; }  // must be public!
    
            // ...
    

提交回复
热议问题