首页 > 解决方案 > EntityType 'Account' 没有定义键。定义此 EntityType 的键

问题描述

我有两个实体类:

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; }
    }
}

我的 DbContext :

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.

我研究并阅读了这个副本。所有属性都设置为公共。任何的想法?

标签: entity-framework

解决方案


您必须[Key]在属性中设置注释,Id并且不要忘记 getter 和 setter:

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

推荐阅读