您的位置:知识库 » 数据库

Linq To Sql进阶系列(三)CUD和Log

作者: Tom Song  来源: 博客园  发布时间: 2008-09-27 11:51  阅读: 13287 次  推荐: 0   原文链接   [收藏]  
[1] Linq To Sql进阶系列(三)CUD和Log
[2] Linq To Sql进阶系列(三)CUD和Log
[3] Linq To Sql进阶系列(三)CUD和Log

系列文章导航:

Linq To Sql进阶系列(一)从映射讲起

Linq To Sql进阶系列(二)M:M关系

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

INSERT INTO [dbo].[Orders]([CustomerID][EmployeeID][OrderDate][RequiredDate][ShippedDate][ShipVia][Freight],
[
ShipName][ShipAddress][ShipCity][ShipRegion][ShipPostalCode][ShipCountry])
VALUES
 (@p0@p1@p2@p3@p4@p5@p6@p7@p8@p9@p10@p11@p12)

SELECT [t0].[OrderID]
FROM [dbo].[Orders] AS [t0]
WHERE [t0].[OrderID] = (SCOPE_IDENTITY())

-- @p0: Input StringFixedLength (Size = 5; Prec = 0; Scale = 0) []
--
 @p1: Input Int32 (Size = 0; Prec = 0; Scale = 0) []
--
 @p2: Input DateTime (Size = 0; Prec = 0; Scale = 0) []
--
 @p3: Input DateTime (Size = 0; Prec = 0; Scale = 0) []
--
 @p4: Input DateTime (Size = 0; Prec = 0; Scale = 0) []
--
 @p5: Input Int32 (Size = 0; Prec = 0; Scale = 0) []
--
 @p6: Input Currency (Size = 0; Prec = 19; Scale = 4) []
--
 @p7: Input String (Size = 0; Prec = 0; Scale = 0) []
--
 @p8: Input String (Size = 4; Prec = 0; Scale = 0) [Test]
--
 @p9: Input String (Size = 0; Prec = 0; Scale = 0) []
--
 @p10: Input String (Size = 0; Prec = 0; Scale = 0) []
--
 @p11: Input String (Size = 0; Prec = 0; Scale = 0) []
--
 @p12: Input String (Size = 0; Prec = 0; Scale = 0) []
--
 Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.20706.1

这表明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();

 

0
0

数据库热门文章

    数据库最新文章

      最新新闻

        热门新闻