问题
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