0
假設我們有一個收集Payments
的Customer
物品。向收藏中添加和保存新物品
初始化:
var dataContext = new TestDataContext();
dataContext.Customers.InsertOnSubmit(new Customer { Id = 1, Name = "Customer1" });
dataContext.SubmitChanges();
var customer = dataContext.Customers.Where(c => c.Id == 1).First();
第一種情況:
customer.Payments.Add(new Payment { Amount = 100, CustomerId = customer.Id });
dataContext.SubmitChanges();
var count = dataContext.Payments.Count(); // count == 0
第二種情況:
dataContext.Payments.InsertOnSubmit(new Payment { Amount = 100, Customer = customer });
dataContext.SubmitChanges();
var count = dataContext.Payments.Count(); // count == 1
第三種情況(組合):
customer.Payments.Add(new Payment { Amount = 100, CustomerId = customer.Id });
dataContext.Payments.InsertOnSubmit(new Payment { Amount = 100, Customer = customer });
dataContext.SubmitChanges();
var count = dataContext.Payments.Count(); // count == 2 (!)
我假設InsertOnSubmit
以某種方式通知DataContext
對象有關更改。但只是想知道爲什麼它沒有在第一個案件通知?
P.S.我正在爲Windows Phone使用SQL CE。