0

У меня есть две таблицы
Как перенести первичный ключ на непервичный ключ в EF-код?

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 ???

ответ

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».

+0

Это не то, что мне нужно @RhymechaeT – RollerCosta

+0

Должно быть, я неправильно понял. Вы хотели связать эти две таблицы с помощью «table1ID»? –