我有一个Linq to SQL实体,它发出以下两个sql语句:
UPDATE [identity].[AddressTypes]
SET [Name] = @p4
WHERE ([SurrogateKey] = @p0) AND ([Name] = @p1) AND ([LastUpdatedOn] = @p2) AND ([LastUpdatedBy] = @p3)
-- @p0: Input UniqueIdentifier (Size = 0; Prec = 0; Scale = 0) [b0cf44d9-c6ba-de11-b194-001e37f334ea]
-- @p1: Input VarChar (Size = 11; Prec = 0; Scale = 0) [Residencial]
-- @p2: Input DateTime (Size = 0; Prec = 0; Scale = 0) [16/10/2009 23:43:28]
-- @p3: Input NVarChar (Size = 23; Prec = 0; Scale = 0) [LUCIANO-NOTEBOO\Luciano]
-- @p4: Input VarChar (Size = 4; Prec = 0; Scale = 0) [Home]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.1
UPDATE [identity].[AddressTypes]
SET [Name] = @p4
WHERE ([SurrogateKey] = @p0) AND ([Name] = @p1) AND ([LastUpdatedOn] = @p2) AND ([LastUpdatedBy] = @p3)
-- @p0: Input UniqueIdentifier (Size = 0; Prec = 0; Scale = 0) [b1cf44d9-c6ba-de11-b194-001e37f334ea]
-- @p1: Input VarChar (Size = 9; Prec = 0; Scale = 0) [Comercial]
-- @p2: Input DateTime (Size = 0; Prec = 0; Scale = 0) [16/10/2009 23:43:29]
-- @p3: Input NVarChar (Size = 23; Prec = 0; Scale = 0) [LUCIANO-NOTEBOO\Luciano]
-- @p4: Input VarChar (Size = 8; Prec = 0; Scale = 0) [Business]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.1
但我需要实体发出如下所示的sql语句,其中LastUpdatedOn和LastUpdatedBy设置为默认值。我想我必须为“部分无效更新...”实现可扩展性方法定义,但不知道如何继续。有人可以帮我解决这个问题吗?
UPDATE [identity].[AddressTypes]
SET [Name] = @p4, [LastUpdatedOn] = DEFAULT, [LastUpdatedBy] = DEFAULT
WHERE ([SurrogateKey] = @p0) AND ([Name] = @p1) AND ([LastUpdatedOn] = @p2) AND ([LastUpdatedBy] = @p3)
-- @p0: Input UniqueIdentifier (Size = 0; Prec = 0; Scale = 0) [b0cf44d9-c6ba-de11-b194-001e37f334ea]
-- @p1: Input VarChar (Size = 11; Prec = 0; Scale = 0) [Residencial]
-- @p2: Input DateTime (Size = 0; Prec = 0; Scale = 0) [16/10/2009 23:43:28]
-- @p3: Input NVarChar (Size = 23; Prec = 0; Scale = 0) [LUCIANO-NOTEBOO\Luciano]
-- @p4: Input VarChar (Size = 4; Prec = 0; Scale = 0) [Home]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.1
UPDATE [identity].[AddressTypes]
SET [Name] = @p4, [LastUpdatedOn] = DEFAULT, [LastUpdatedBy] = DEFAULT
WHERE ([SurrogateKey] = @p0) AND ([Name] = @p1) AND ([LastUpdatedOn] = @p2) AND ([LastUpdatedBy] = @p3)
-- @p0: Input UniqueIdentifier (Size = 0; Prec = 0; Scale = 0) [b1cf44d9-c6ba-de11-b194-001e37f334ea]
-- @p1: Input VarChar (Size = 9; Prec = 0; Scale = 0) [Comercial]
-- @p2: Input DateTime (Size = 0; Prec = 0; Scale = 0) [16/10/2009 23:43:29]
-- @p3: Input NVarChar (Size = 23; Prec = 0; Scale = 0) [LUCIANO-NOTEBOO\Luciano]
-- @p4: Input VarChar (Size = 8; Prec = 0; Scale = 0) [Business]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.1
此致
答案 0 :(得分:2)
只有在 INSERT 新记录时才会应用SQL Server中的DEFAULT值。当您更新现有记录时,它们永远不会被应用。
如果您希望每次更新都更新LastUpdateOn
和LastUpdateBy
列,则可能需要使用触发器来处理。或者将它们作为实体/实体的属性公开,这样您就可以在执行update语句之前以编程方式设置它们。
Linq-to-SQL为您提供了许多要加入的扩展点 - 例如实体类上的OnValidate
方法:
partial void OnValidate(System.Data.Linq.ChangeAction action)
{
if (action == ChangeAction.Update)
{
// set your LastUpdatedBy and LastUpdatedOn values here
}
}
或者您可以在类中实现部分方法OnUpdate(Entity)
并根据需要设置字段:
partial void UpdateEmployee(Employee instance)
{
// set your LastUpdatedBy and LastUpdatedOn values here
ExecuteDynamicUpdate(instance);
}
使用实用程序类进行审核的另一个选项解释为here。
马克
答案 1 :(得分:0)
我在数据库级别使用触发器更好地处理了我的问题,因为我需要自动更新这些字段并使用服务器端信息(日期/时间)。
CREATE TRIGGER [identity].[AddressTypesInsteadOfUpdate]
ON [identity].[AddressTypes]
INSTEAD OF UPDATE
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for trigger here
UPDATE [identity].AddressTypes
SET Name = inserted.Name,
LastUpdatedOn = DEFAULT,
LastUpdatedBy = DEFAULT
FROM [identity].AddressTypes
INNER JOIN
inserted
ON [identity].AddressTypes.SurrogateKey = inserted.SurrogateKey
END