有的时候我们会把对象从外部传入 DataContext,要求它更新,由于不同的 DataContext
是相对独立的。由于新的 DataContext 中还没有获取实体,我们只能通过附加方式更新数
据。
首先把 Customer 表的主键字段加上 IsVersion 标识:
[Column(Storage="_CustomerID", DbType="NChar(5) NOT NULL",
CanBeNull=false, IsPrimaryKey=true, IsVersion = true)]
运行下面的测试代码:
Customer c = new Customer { CustomerID = "ALFKI", ContactName = "zhuye",
CompanyName = "1111" };
ctx.Customers.Attach(c, true);
ctx.SubmitChanges();
会捕捉到下面的 SQL 语句:
UPDATE [dbo].[Customers]
SET [CompanyName] = @p2, [ContactName] = @p3, [ContactTitle] = @p4, [Address] =
@p5, [City] = @p6, [Region] = @p7, [PostalCode] = @p8, [Country] = @p9, [Phone] =
@p10, [Fax] = @p11
WHERE ([CustomerID] = @p0) AND ([CustomerID] = @p1)
-- @p0: Input StringFixedLength (Size = 5; Prec = 0; Scale = 0) [ALFKI]
-- @p1: Input String (Size = 5; Prec = 0; Scale = 0) [ALFKI]
-- @p2: Input String (Size = 4; Prec = 0; Scale = 0) [1111]
-- @p3: Input String (Size = 5; Prec = 0; Scale = 0) [zhuye]
-- @p4: Input String (Size = 0; Prec = 0; Scale = 0) []
-- @p5: Input String (Size = 0; Prec = 0; Scale = 0) []
-- @p6: Input String (Size = 0; Prec = 0; Scale = 0) []
-- @p7: Input String (Size = 0; Prec = 0; Scale = 0) []
-- @p8: Input String (Size = 0; Prec = 0; Scale = 0) []
-- @p9: Input String (Size = 0; Prec = 0; Scale = 0) []
-- @p10: Input String (Size = 0; Prec = 0; Scale = 0) []
是相对独立的。由于新的 DataContext 中还没有获取实体,我们只能通过附加方式更新数
据。
首先把 Customer 表的主键字段加上 IsVersion 标识:
[Column(Storage="_CustomerID", DbType="NChar(5) NOT NULL",
CanBeNull=false, IsPrimaryKey=true, IsVersion = true)]
运行下面的测试代码:
Customer c = new Customer { CustomerID = "ALFKI", ContactName = "zhuye",
CompanyName = "1111" };
ctx.Customers.Attach(c, true);
ctx.SubmitChanges();
会捕捉到下面的 SQL 语句:
UPDATE [dbo].[Customers]
SET [CompanyName] = @p2, [ContactName] = @p3, [ContactTitle] = @p4, [Address] =
@p5, [City] = @p6, [Region] = @p7, [PostalCode] = @p8, [Country] = @p9, [Phone] =
@p10, [Fax] = @p11
WHERE ([CustomerID] = @p0) AND ([CustomerID] = @p1)
-- @p0: Input StringFixedLength (Size = 5; Prec = 0; Scale = 0) [ALFKI]
-- @p1: Input String (Size = 5; Prec = 0; Scale = 0) [ALFKI]
-- @p2: Input String (Size = 4; Prec = 0; Scale = 0) [1111]
-- @p3: Input String (Size = 5; Prec = 0; Scale = 0) [zhuye]
-- @p4: Input String (Size = 0; Prec = 0; Scale = 0) []
-- @p5: Input String (Size = 0; Prec = 0; Scale = 0) []
-- @p6: Input String (Size = 0; Prec = 0; Scale = 0) []
-- @p7: Input String (Size = 0; Prec = 0; Scale = 0) []
-- @p8: Input String (Size = 0; Prec = 0; Scale = 0) []
-- @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) []