breeze - 在保存之前,根据服务器的导航属性修改服务器上的实体

时间:2016-03-31 18:20:18

标签: breeze

在服务器端的微风中,有没有办法在BeforeSaveEntity(或保存前的任何其他地方)获取Entity的导航属性的“当前”值?顺便说一句,我的意思是数据库中存在的内容,任何传入的更改都合并在一起。这不是用于验证,而是我基于两者计算父属性(我不想在客户端上)的值。父字段和子字段...

例如,

public class Parent {
  public ICollection<Child> Children{ get; set; }
}

。 。

protected override bool BeforeSaveEntity(EntityInfo entityInfo) {
  if (entityInfo.Entity.GetType() == typeof(Parent) &&
  (entityInfo.EntityState == EntityState.Added || entityInfo.EntityState == EntityState.Updated)) {

   // Lazy load Parent's Children collection out of breeze's context 
   // so items are "current' (existing merged with changes)

   Parent parent = (Parent)entityInfo.Entity;
   Context.Entry(parent).Collection(p => p.Children).Load();

   // this throws exception Member 'Load' cannot be called for property
   // 'Children' because the entity of type 'Parent' does not exist in the context.
  }
}  

我认为他们还没有进入DBContext。我所能想到的就是从数据库中检索现有的子项,并手动合并BeforeSaveEntities中的更改,这很麻烦。

1 个答案:

答案 0 :(得分:1)

在Breeze用于保存的DbContext中未启用延迟加载。原因详见this SO answer

您应该在separate DbContext中加载任何其他实体。

以下是我在项目中如何做到这一点的一个例子。也许MergeEntities和DetachEntities方法应该包含在Breeze中,以便更容易实现。

protected override Dictionary<Type, List<EntityInfo>> BeforeSaveEntities(Dictionary<Type, List<EntityInfo>> saveMap)
{
    // create a separate context for computation, so we don't pollute the main saving context
    using (var newContext = new MyDbContext(EntityConnection, false))
    {
        var parentFromClient = (Parent)saveMap[typeof(Parent)][0].Entity;

        // Load the necessary data into the newContext
        var parentFromDb = newContext.Parents.Where(p => p.ParentId == parentFromClient.ParentId)
            .Include("Children").ToList();

        // ... load whatever else you need...

        // Attach the client entities to the ObjectContext, which merges them and reconnects the navigation properties
        var objectContext = ((IObjectContextAdapter)newContext).ObjectContext;
        var objectStateEntries = MergeEntities(objectContext, saveMap);

        // ... perform your business logic...

        // Remove the entities from the second context, so they can be saved in the original context
        DetachEntities(objectContext, saveMap);
    }
    return saveMap;
}

/// Attach the client entities to the ObjectContext, which merges them and reconnects the navigation properties
Dictionary<ObjectStateEntry, EntityInfo> MergeEntities(ObjectContext oc, Dictionary<Type, List<EntityInfo>> saveMap)
{
    var oseEntityInfo = new Dictionary<ObjectStateEntry, EntityInfo>();
    foreach (var type in saveMap.Keys)
    {
        var entitySet = this.GetEntitySetName(type);
        foreach(var entityInfo in saveMap[type])
        {
            var entityKey = oc.CreateEntityKey(entitySet, entityInfo.Entity);
            ObjectStateEntry ose;
            if (oc.ObjectStateManager.TryGetObjectStateEntry(entityKey, out ose))
            {
                if (ose.State != System.Data.Entity.EntityState.Deleted)
                    ose.ApplyCurrentValues(entityInfo.Entity);
            }
            else
            {
                oc.AttachTo(entitySet, entityInfo.Entity);
                ose = oc.ObjectStateManager.GetObjectStateEntry(entityKey);
            }

            if (entityInfo.EntityState == Breeze.ContextProvider.EntityState.Deleted)
            {
                ose.Delete();
            }
            oseEntityInfo.Add(ose, entityInfo);
        }
    }
    return oseEntityInfo;
}

/// Remove the entities in saveMap from the ObjectContext; this separates their navigation properties
static void DetachEntities(ObjectContext oc, Dictionary<Type, List<EntityInfo>> saveMap)
{
    foreach (var type in saveMap.Keys)
    {
        foreach (var entityInfo in saveMap[type])
        {
            try
            {
                oc.Detach(entityInfo.Entity);
            }
            catch
            { // the object cannot be detached because it is not attached
            }
        }
    }
}