代码之家  ›  专栏  ›  技术社区  ›  Abdus Sattar Bhuiyan

EntityType“Account”没有定义键。定义此EntityType的键

  •  0
  • Abdus Sattar Bhuiyan  · 技术社区  · 6 年前

    我有两个实体类:

    namespace PowerSupply.Domain
    {
        [Table("Accounts")]
        public class Account
        {
            public int Id;
            public string CompanyName;
            public float Interval;
        }
    }
    

    以及

    namespace PowerSupply.Domain
    {
       public class ContactInfo
        {
            public int Id { get; set; }
            public string mobileNo { get; set; }
            public virtual Account Account { get; set; }
        }
    }
    

    以及我的背景:

    public class PowerSupplyDBContext : DbContext
    {
        public PowerSupplyDBContext() : base("PowerSupplyDatabase")
        {
    
        }
    
        public DbSet<Account> Acounts { get; set; }
        public DbSet<ContactInfo> ContactInfo { get; set; }
    }
    

    One or more validation errors were detected during model generation:
    
    PowerSupply.Persistance.Facade.Account: : EntityType 'Account' has no key defined. Define the key for this EntityType.
    Acounts: EntityType: EntitySet 'Acounts' is based on type 'Account' that has no keys defined.
    

    我学习和阅读 this 重复。

    1 回复  |  直到 6 年前
        1
  •  1
  •   JPocoata    6 年前

    你必须把 [Key] 中的批注 Id

    namespace PowerSupply.Domain
    {
        [Table("Accounts")]
        public class Account
        {
            [Key]
            public int Id {get; set;}
            public string CompanyName {get; set;}
            public float Interval {get; set}
        }
    }