Reputation: 7525
I have a javascript application that saves changes in the front end, pushes them to a WebAPI controller and saves them to an Entity Framework context. In order to save a history of the changes made to the table I would like to intercept certain edits to certain tables and change them from an edit row type command to a new row command with the edited data. My current controller looks like this
[HttpPost]
public SaveResult SaveChanges(JObject saveBundle)
{
return _ContextProvider.SaveChanges(saveBundle);
}
How can I set this up to create the rows?
Upvotes: 1
Views: 256
Reputation: 17052
Update the 'saveMap' that is part of the BeforeSaveEntities delegate or overridden method. Use the ContextProvider's CreateEntityInfo method to create items to add to the saveMap.
public SaveResult SaveChanges(JObject saveBundle) {
_ContextProvider.BeforeSaveEntitiesDelegate = CreateNewFoo;
return _ContextProvider.SaveChanges(saveBundle);
}
// the saveMap parameter contains the entities that were passed from the client to be saved
// you can add to the map here:
private Dictionary<Type, List<EntityInfo>> CreateNewFoo(Dictionary<Type, List<EntityInfo>> saveMap) {
// create your new entity.
var foo = new Foo();
foo.CreatedOn = DateTime.Now;
// SaveOptions.Tag is free form data that can be passed from the client.
var tag = ContextProvider.SaveOptions.Tag;
foo.Comment = (tag == null) ? "Generic comment" : tag.ToString();
// create an EntityInfo object from the new entity
var ei = ContextProvider.CreateEntityInfo(foo);
// add it to the saveMap
List<EntityInfo> fooInfos;
if (!saveMap.TryGetValue(typeof(Foo), out fooInfos)) {
fooInfos = new List<EntityInfo>();
saveMap.Add(typeof(Foo), fooInfos);
}
fooInfos.Add(ei);
// return the updated saveMap
return saveMap;
}
Upvotes: 2