0
我有一個有趣的問題,我試圖插入嵌套數據使用l2s,一切都好,如果我不嘗試插入實體fk也根父,樣本模式:嵌套LINQ到SQL插入的外鍵問題
所有ID是PK身份
測試代碼:
Models.testdbDataContext db = new Models.testdbDataContext();
List<string> data = new List<string>();
data.Add("kkkk1");
data.Add("kkkk2");
data.Add("kkkk3");
data.Add("kkkk4");
data.Add("kkkk5");
foreach (var item in data)
{
nested1 n1 = new nested1();
n1.name1 = "test1";
db.nested1.InsertOnSubmit(n1);
foreach (var item2 in data)
{
nested2 n2 = new nested2();
n2.name2 = "test2";
n1.nested2.Add(n2);
foreach (var item3 in data)
{
nested3 n3 = new nested3();
n3.name3 = "test3";
n2.nested3.Add(n3);
}
}
}
db.SubmitChanges(); <-- error here (duh)
錯誤:「INSERT語句與衝突FOREIGN KEY約束「FK_nested3_nested1」。衝突發生在數據庫「TESTDB.MDF」,表「dbo.nested1」,列'id'中。 該語句已終止。」
問題是,如果表nested3也FK到nested1 ID這個錯誤發生,當nested3已FK只 其父nested2 ID是沒有問題的,似乎L2S可以」 T單獨從以前的父母得到nested1身份。
那麼如何克服呢?