我有两个类。如何使用 Entity Framework 代码优先方法将这两个类变成一对一的关系?
public class Region
{
public int RegionId { get; set; }
public string Name { get; set; }
public virtual Factory _factory { get; set; }
}
public class Factory
{
public int FactoryId { get; set; }
public string Name { get; set; }
public virtual Region _region { get; set; }
}
当我尝试这个时,我得到这个错误:
多重性在关系“Region_Factory”中的角色“Region_Factory_Source”中无效。因为从属角色属性不是关键属性,所以从属角色的多重性的上限必须是“*”。
最佳答案
EF6,添加属性:
[Key]
public int RegionId { get; set; }
[Key, ForeignKey("Region")]
public int FactoryId { get; set; }
关于entity-framework - Entity Framework 1 对 1 关系首先使用代码。如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18724964/