ASP.NET MVC 学习笔记 (3)
学习NerdDinner 时,InsertOnSubmit方法执行后怎么也插入不到数据库中,查找MSDN,找到了原因,原来还要执行一下SubmitChanges方法。
1 // Create a new Order object.
2 Order ord = new Order
3 {
4 OrderID = 12000,
5 ShipCity = "Seattle",
6 OrderDate = DateTime.Now
7 // …
8 };
9
10 // Add the new object to the Orders collection.
11 db.Orders.InsertOnSubmit(ord);
12
13 // Submit the change to the database.
14 try
15 {
16 db.SubmitChanges();
17 }
18 catch (Exception e)
19 {
20 Console.WriteLine(e);
21 // Make some adjustments.
22 // ...
23 // Try again.
24 db.SubmitChanges();
25 }