Error: the entity type requires a primary key

后端 未结 11 1586
走了就别回头了
走了就别回头了 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:48

    I found a bit different cause of the error. It seems like SQLite wants to use correct primary key class property name. So...

    Wrong PK name

    public class Client
    {
      public int SomeFieldName { get; set; }  // It is the ID
      ...
    }
    

    Correct PK name

    public class Client
    {
      public int Id { get; set; }  // It is the ID
      ...
    }
    
    public class Client
    {
      public int ClientId { get; set; }  // It is the ID
      ...
    }
    

    It still posible to use wrong PK name but we have to use [Key] attribute like

    public class Client
    {
       [Key]
       public int SomeFieldName { get; set; }  // It is the ID
       ...
    }
    

提交回复
热议问题