如何首先将主键映射到EF代码中的非主键?

时间:2012-05-09 10:28:33

标签: asp.net asp.net-mvc-3 entity-framework linq-to-entities ef-code-first

我有两张桌子

public class Table1
{
[Key]
int ID{get; set;}
Table2 table2{get; set;}
}
public class Table2
{
[Key]
int ID{get; set;}
int table1ID{get; set;}
//no instance of table i.e is relationship is uni-directional only
}


如何设置table1与table2的关系与Table1.ID和Table2.table1ID ???

1 个答案:

答案 0 :(得分:0)

以此为例:

public class Table1
{
[Key]
int ID{get; set;}
Table2 table2{get; set;}
}
public class Table2
{
[Key]
int ID{get; set;}
int table1ID{get; set;}

[ForeignKey("table1ID")]
Table1 table1{get; set;}
//no instance of table i.e is relationship is uni-directional only
}

这意味着Table2有一个Table1引用(table1),它的外键由ForeignKeyAttribute指定为“table1ID”。