自引用对象和Breeze.js SaveChanges

时间:2014-08-28 20:35:58

标签: entity-framework breeze

我删除了一些已删除对象的子节点,以通过覆盖我派生的EFContextProvider类中的BeforeSaveEntity来保留外键冲突:

protected override bool BeforeSaveEntity(EntityInfo entityInfo)
{
    dbEntities db = new dbEntities();
    if (entityInfo.Entity.GetType() == typeof(ServerFile) && entityInfo.EntityState == EntityState.Deleted)
    {
         ServerFile filetodelete = entityInfo.Entity as ServerFile;
         if (filetodelete.OriginalFileID == null)
         {
             IEnumerable<ServerFile> f = db.ServerFiles.Where(x => x.OriginalFileID == filetodelete.ID);
             foreach (ServerFile fd in f)
                db.ServerFiles.Remove(fd);
         }
    }
    db.SaveChanges();
}

在我使用[BreezeController]归因的派生ApiController类中,我因此刷新了我的上下文:

public SaveResult SaveChanges(JObject saveBundle)
{
    _contextProvider.ObjectContext.Refresh(RefreshMode.StoreWins, _contextProvider.ObjectContext.ObjectStateManager.GetObjectStateEntries(System.Data.Entity.EntityState.Deleted));
    return _contextProvider.SaveChanges(saveBundle);
}

但我仍然得到错误:

An exception of type 'System.Exception' occurred in Breeze.ContextProvider.dll 
but was not handled in user code

Additional information: Store update, insert, or delete statement affected an
unexpected number of rows (0). Entities may have been modified or deleted since
entities were loaded. Refresh ObjectStateManager entries.

1 个答案:

答案 0 :(得分:0)

您的数据库上没有触发器吗?我已经看到错误是由过去基础表上的触发器引起的。