我试图将删除命令更改为更新命令。但是以下一行
DbSetClause clause = DbExpressionBuilder.SetClause(property, now);
给了我一个例外。
{"Method not found: 'System.Data.Entity.Core.Common.CommandTrees.DbSetClause System.Data.Entity.Core.Common.CommandTrees.ExpressionBuilder.DbExpressionBuilder.SetClause(System.Data.Entity.Core.Common.CommandTrees.DbExpression, System.Data.Entity.Core.Common.CommandTrees.DbExpression)'."}
它假设是一个非常直接的代码。我不明白为什么它不起作用。
var deleteCommand = interceptionContext.OriginalResult as DbDeleteCommandTree;
if (deleteCommand != null)
{
var column = "DeleteTime";
if (column != null)
{
var setClauses = new List<DbModificationClause>();
var table = (EntityType)deleteCommand.Target.VariableType.EdmType;
if (table.Properties.Any(p => p.Name == column))
{
DbVariableReferenceExpression variable = DbExpressionBuilder.Variable(deleteCommand.Target.VariableType, deleteCommand.Target.VariableName);
DbPropertyExpression property = DbExpressionBuilder.Property(variable, column);
DbExpression now = DbExpression.FromDateTime(DateTime.Now);
DbSetClause clause = DbExpressionBuilder.SetClause(property, now);
setClauses.Add(clause);
}
var update = new DbUpdateCommandTree(
deleteCommand.MetadataWorkspace,
deleteCommand.DataSpace,
deleteCommand.Target,
deleteCommand.Predicate,
setClauses.AsReadOnly(),
null);
interceptionContext.Result = update;
}
}