Error: the entity type requires a primary key

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

    Yet another reason may be that your entity class has several properties named somhow /.*id/i - so ending with ID case insensitive AND elementary type AND there is no [Key] attribute.

    EF will namely try to figure out the PK by itself by looking for elementary typed properties ending in ID.

    See my case:

    public class MyTest, IMustHaveTenant
    {
      public long Id { get; set; }
      public int TenantId { get; set; }
      [MaxLength(32)]
      public virtual string Signum{ get; set; }
      public virtual string ID { get; set; }
      public virtual string ID_Other { get; set; }
    }
    

    don't ask - lecacy code. The Id was even inherited, so I could not use [Key] (just simplifying the code here)

    But here EF is totally confused.

    What helped was using modelbuilder this in DBContext class.

                modelBuilder.Entity<MyTest>(f =>
                {
                    f.HasKey(e => e.Id);
                    f.HasIndex(e => new { e.TenantId });
                    f.HasIndex(e => new { e.TenantId, e.ID_Other });
                });
    

    the index on PK is implicit.

    0 讨论(0)
  • 2020-12-02 15:55

    The entity type 'DisplayFormatAttribute' requires a primary key to be defined.

    In my case I figured out the problem was that I used properties like this:

    public string LastName { get; set; }  //OK
    public string Address { get; set; }   //OK 
    public string State { get; set; }     //OK
    public int? Zip { get; set; }         //OK
    public EmailAddressAttribute Email { get; set; } // NOT OK
    public PhoneAttribute PhoneNumber { get; set; }  // NOT OK
    

    Not sure if there is a better way to solve it but I changed the Email and PhoneNumber attribute to a string. Problem solved.

    0 讨论(0)
  • 2020-12-02 15:55

    Make sure you have the following condition:

    1. Use [key] if your primary key name is not Id or ID.
    2. Use the public keyword.
    3. Primary key should have getter and setter.

    Example:

    public class MyEntity {
       [key]
       public Guid Id {get; set;}
    }
    
    0 讨论(0)
  • 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!
    
            // ...
    
    0 讨论(0)
  • 2020-12-02 15:57

    This exception message doesn't mean it requires a primary key to be defined in your database, it means it requires a primary key to be defined in your class.

    Although you've attempted to do so:

    private Guid _id;
    [Key]
    public Guid ID
    {
        get { return _id; }
    }
    

    This has no effect, as Entity Framework ignores read-only properties. It has to: when it retrieves a Fruits record from the database, it constructs a Fruit object, and then calls the property setters for each mapped property. That's never going to work for read-only properties.

    You need Entity Framework to be able to set the value of ID. This means the property needs to have a setter.

    0 讨论(0)
提交回复
热议问题