2010-05-02 67 views
0

我有一個表(TestTable的),例如:如何使用linq插入實體外鍵值?

ID 
Name 
Parentid (FK) 

,我想插入像ID(1)名稱(測試)PARENTID(5)FK的新紀錄。如何用linq將新記錄插入TestTable到實體?

var testTable = new TestTable(); 
testTable.ID = 1; 
testTable.Name = "TestName"; 
testTable ... 

感謝您的工作示例。

回答

0

試試這個:

using (var context = new MyEntities())//MyEntities name may differ 
{ 
    var testTable = new TestTable(); 
    testTable.ID = 1; 
    testTable.Name = "TestName"; 
    testTable.ParentTableReference.EntityKey = EntityKey("MyEntities.ParentTableSet", entityKeyValues);//Entity set name for parent table may differ 

    context.AddObject("TestTableSet",testTable);//Entity set name for TestTable may differ 
    context.SaveChanges(); 
}