我有這個打算我瘋了,的LINQ to SQL的SubmitChanges()插入兩個數據庫行和一個子行
我附加了一個列表1個客戶和1個地址子記錄行。 調試時一切似乎都正常。應該插入1個客戶行和1個地址行。 但是,我得到2個客戶記錄和1個地址行。
我不知道爲什麼。當在List中連接和循環時,只會看到1條記錄。
任何點?
將帖子
代碼附:
public bool InsertUpdateCustomers(List<Customer> customerList, List<Customer> originalCustomers)
{
using (DbContext db = new DbContext(DbContext.ConnectionString))
{
db.Log = Console.Out;
List<Customer> customerCloned = new List<Customer>();
customerList.ForEach(p => customerCloned.Add(p.CloneObjectGraph()));
customerCloned.ForEach(p => p.Address =
customerList.Where(pe => pe.Id == p.Id).Single().Address.CloneObjectGraph());
customerCloned.ForEach(p =>
{
if (p.Id > 0)
{
db.Customer.Attach(p,
originalCustomers.Single(
x => x.Id == p.Id));
db.Address.Attach(p.Address,
originalCustomers.Single(
x => p.AddressId== x.AddressId).
Address);
}
});
customerCloned.ForEach(p =>
{
if (p.Id == 0)
db.Customer.InsertOnSubmit(p);
});
try
{
db.SubmitChanges(ConflictMode.ContinueOnConflict);
return true;
}
catch (Exception ex)
{
return false;
}
}
}
我在輸出檢查日誌,我看到的確是2個插入表中。 我沒有看到任何關於地址,但插入正確。
這可能是我不明白的外鍵問題。
附上代碼示例bro。 – hydrogen 2010-09-03 23:01:06
請1分鐘。 – AlwaysBeCoding 2010-09-03 23:06:29
什麼是原創客戶? – DEVMBM 2010-09-06 02:01:44