EntityType“帐户”没有定义键。定义此EntityType的键

时间:2018-09-27 13:23:37

标签: entity-framework

我有两个实体类:

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.

我研究并阅读了this个副本。 所有财产均设为公开。有想法吗?

1 个答案:

答案 0 :(得分:1)

您必须在[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}
    }
}