我知道那里有几个类似的帖子,但我找不到任何解决这个问题的方法.
我想在Entity Framework 6中添加,更改或删除实体(软删除)时添加(某种)AudioLog.我已经重写了SaveChanges,因为我只想为EntityStates添加,修改或删除的日志条目,我在第一次调用SaveChanges之前获取列表.问题是,因为我需要记录已执行的操作,我需要检查实体的EntityState.但是在调用SaveChanges之后,所有条目的EntityState都是Unchanged.
public override int SaveChanges()
{
using (var scope = new TransactionScope())
{
var modifiedEntries = ChangeTracker.Entries()
.Where(e => e.State == EntityState.Added || e.State == EntityState.Deleted || e.State == EntityState.Modified)
.ToList();
int changes = base.SaveChanges();
foreach (var entry in modifiedEntries)
{
ApplyAuditLog(entry);
}
base.SaveChanges();
scope.Complete();
return changes;
}
}
private void ApplyAuditLog(DbEntityEntry entry)
{
ILog entity = entry.Entity as ILog;
if (entity != null)
{
LogOperation operation;
switch (entry.State)
{
case EntityState.Added:
operation = LogOperation.CreateEntity;
break;
case …Run Code Online (Sandbox Code Playgroud) c# entity-framework savechanges audit-logging entity-framework-6
我使用Lambda表达式构建了一个存储库来过滤我的实体集合.作为我发送的方法的参数Expression<Func<Case, bool>> exp.但是在方法中我想用一些全局过滤器更新同一个表达式.我可以看到表达式对象本身有一个Update方法,但我无法弄清楚它是如何实现的(在搜索网络时找不到任何东西).
exp.Update(exp.Body, ???);
Run Code Online (Sandbox Code Playgroud)
谁能举个例子?
编辑:方法的定义:http://msdn.microsoft.com/en-us/library/ee378255.aspx
EDIT2:这是我的代码(我尝试使用.And):
Expression<Func<Case, bool>> newExp = c => c.CaseStatusId != (int)CaseStatus.Finished
var binExp = Expression.And(exp.Body, newExp.Body);
ParameterExpression paramExp = Expression.Parameter(typeof(Expression<Func<Case, bool>>), "c");
return repository.Where(Expression.Lambda<Expression<Func<Case, bool>>>(binExp,
new[] { paramExp }).Compile()).ToArray();
Run Code Online (Sandbox Code Playgroud)
它失败并出现以下ArgumentException:Lambda类型参数必须从System.Delegate派生