Breeze BeforeSaveEntities: how to modify savemap

帅比萌擦擦* 提交于 2019-12-02 08:53:24

问题


I read the following from the Breeze documentation about BeforeSaveEntities:

"Entities may be added or removed from the map returned by this method".

So I suppose I can add a new instance of EntityInfo to the saveMap. My question is: how can I do that? Is there any example of that anywhere?

I can perfectly loop through the dictionary. But since EntityInfo has no constructor, and all its fields are get only, I feel a bit stuck here. Any help is welcome.

Thanks


回答1:


Ok, here is a very contrived example of a BeforeSaveEntities override that creates comment records alongside a whatever is normally saved. Comment records include a comment generated based on the value of the SaveOptions.Tag property.

protected override Dictionary<Type, List<EntityInfo>> BeforeSaveEntities(Dictionary<Type,   List<EntityInfo>> saveMap) {
    var comment = new Comment();
    var tag = ContextProvider.SaveOptions.Tag;
    comment.Comment1 = (tag == null) ? "Generic comment" : tag.ToString();
    comment.CreatedOn = DateTime.Now;
    comment.SeqNum = 1;
    var ei = ContextProvider.CreateEntityInfo(comment);
    List<EntityInfo> comments;
    if (!saveMap.TryGetValue(typeof(Comment), out comments)) {
      comments = new List<EntityInfo>();
      saveMap.Add(typeof(Comment), comments);
    }
    comments.Add(ei);

    return saveMap;
  }

}




回答2:


This answer is for those developers that have chosen to use Database First using an objectContext instead of Code First, and for Nicolas.

I found after using the Breeze Source Code in Debug that line 805 of the GetEntitySetName method (cspaceEntityType = cspaceEntityTypes.First(et => et.FullName == entityType.FullName)

I would get the error "Sequence contains no matching element"

I noticed inside my watch that et.FullName and entityType.FullName did not have the same namespace. This told my comrad and I that the edmx models namespace was not the same as the object context.

Go to your edmx model select right click inside the empty space and select properties. ensure that the Namespace property is the same as your Object Context.



来源:https://stackoverflow.com/questions/17163085/breeze-beforesaveentities-how-to-modify-savemap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!