我使用Breeze和我的Web API,我想在我的实体上添加一些审核。所以我想使用BeforeSaveEntityDelegate但是当我实现它时,它会在save函数之后被触发... 这是我的Breezecontroller:
[BreezeController]
public class EssController : ApiController
{
private readonly ESSContextProvider _contextProvider;
public EssController(ESSContextProvider contextProvider)
{
_contextProvider = contextProvider;
}
protected bool BeforeSaveEntity(EntityInfo entityInfo)
{
// create audit record and add to your instance of your context
// this.Context.YourAuditEntity.Add(...)
if (entityInfo.EntityState == EntityState.Modified)
{
var auditable = (Entity)entityInfo.Entity;
auditable.UpdatedBy = "jja";
auditable.UpdatedDate = DateTime.Now;
}
return true;
}
[HttpPost]
public SaveResult SaveChanges(JObject saveBundle)
{
_contextProvider.BeforeSaveEntityDelegate = BeforeSaveEntity;
return _contextProvider.SaveChanges(saveBundle);
}
[HttpGet]
public string Metadata()
{
return _contextProvider.Metadata();
}
}
首先他执行SaveChanges然后输入BeforeSaveEntity ......
答案 0 :(得分:0)
我认为调用顺序没有错误。当你调用_contextProvider.SaveChanges(saveBundle);
时,ContextProvider的SaveChanges方法将调用BeforeSaveEntityDelegate(如果有的话),那么它将保留更改并调用AfterSaveEntities委托。
在这里查看ContextProvider的源代码https://github.com/IdeaBlade/Breeze/blob/master/Breeze.ContextProvider/ContextProvider.cs。寻找" OpenAndSave"课堂内的方法。
不要将您的ApiController SaveChanges与ContextProvider的SaveChanges混淆。