Nicolas
Nicolas

Reputation: 446

Breeze BeforeSaveEntities: how to modify savemap

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

Upvotes: 1

Views: 1713

Answers (2)

ProModel Sasquatch
ProModel Sasquatch

Reputation: 11

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.

Screenshot

Upvotes: 1

Jay Traband
Jay Traband

Reputation: 17052

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;
  }

}

Upvotes: 5

Related Questions