entity-framework - 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.
我研究并阅读了这个副本。所有属性都设置为公共。任何的想法?
解决方案
您必须[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}
}
}
推荐阅读
- node.js - Webdriverio TypeError:element.click 不是函数
- c# - 错误 1 'System.IO.File.WriteAllBytes(string, byte[])' 的最佳重载方法匹配有一些无效参数
- excel - 如何从嵌入外部系统的 TreeView 获取节点项目标题?
- c# - 数据网格视图 全选
- sql - 用于在数据库中查找用户的 SQL 查询的性能
- reactjs - TypeError:无法读取 JEST 和酶测试中未定义的属性“子路径”
- php - PHP 致命错误:允许的内存大小为 1073741824 字节已用尽(试图分配 16777216 字节)
- java - Java android EventBus 得到两个相同的事件
- intellij-idea - 在 Intellij 中传递“-parameters”选项
- javascript - 使用 Lodash 在 Nodejs 中按多个字段分组