我正在研究POCO实体 模型 </跨度> 在现有数据库上。一张桌子有一张 复合 </跨度> 键
公共类Table1{ [键] public virtual int Key1 {get;组;}
[键] public virtual int Key2 {get ;组;}
公共虚拟ICollection&lt;表2&gt; Tables2 {get;组;}
//这里有更多属性……}
和第二个没有主键的表,但引用了2个属性 复合 </跨度> 表1的关键
是的,您可以使用数据注释定义复合外键:
public class Table2 { [ForeignKey("Table1"), Column(Order = 1)] public virtual int Key1 {get; set;} [ForeignKey("Table1"), Column(Order = 2)] public virtual int Key2 {get; set;} [InverseProperty("Tables2")] public virtual Table1 Table1 {get; set;} //more properties here... }
或者:
public class Table2 { public virtual int Key1 {get; set;} public virtual int Key2 {get; set;} [InverseProperty("Tables2")] [ForeignKey("Key1, Key2")] public virtual Table1 Table1 {get; set;} //more properties here... }
但真正的问题是你的 Table2 没有实体框架所需的主键。我不认为有任何解决方法可以解决此问题 - 除了向表中添加主键。
Table2