Linq To Sql进阶系列(三)CUD和Log
系列文章导航:
Linq To Sql进阶系列(四)User Define Function篇
Linq To Sql进阶系列(五)Store Procedure篇
Linq To Sql进阶系列(六)用object的动态查询与保存log篇
Linq To Sql进阶系列(七)动态查询续及CLR与SQL在某些细节上的差别
你可以扑获如下的sql

[ShipName], [ShipAddress], [ShipCity], [ShipRegion], [ShipPostalCode], [ShipCountry])
VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10, @p11, @p12)




















这表明Linq To Sql自动更新了该对象,把数据库自增字段的值取出,赋于该对象。从这里,也可以看出,Linq To Sql在插入数据时,自动调用了事务,以防止返回的不是其插入的。
2.2
对与One : Many的关系型的,在提交One端新数据时,Linq To Sql会自动将Many端的数据一起提交。注意,是提交One端哦。比如
var newCategory = new Category { CategoryName = "Widgets",
Description = "Widgets are the customer-facing analogues " +
"to sprockets and cogs."
};
var newProduct = new Product { ProductName = "Blue Widget",
UnitPrice = 34.56M,
Category = newCategory
};
db2.Categories.Add(newCategory);
db2.SubmitChanges();
2.3
而对于Many : Many的关系(关于M:M请参考上篇),就需要你从One 一个个开始,一直到Many端,自己去提交了。如:
var newEmployee = new Employee { FirstName = "Kira",
LastName = "Smith"
};
var newTerritory = new Territory { TerritoryID = "12345",
TerritoryDescription = "Anytown",
Region = db.Regions.First()
};
var newEmployeeTerritory = new EmployeeTerritory { Employee = newEmployee,
Territory = newTerritory
};
db.Employees.Add(newEmployee);
db.Territories.Add(newTerritory);
db.EmployeeTerritories.Add(newEmployeeTerritory);
db.SubmitChanges();