secret
Last active

  • Download Gist
ContextProvider.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
protected override Dictionary<Type, List<EntityInfo>> BeforeSaveEntities(Dictionary<Type, List<EntityInfo>> saveMap) {
Dictionary<Type, List<EntityInfo>> saveMapAdditions = new Dictionary<Type, List<EntityInfo>>();
foreach (var type in saveMap.Keys) {
if (type == typeof(Category)) {
foreach (var entityInfo in saveMap[type]) {
if (entityInfo.EntityState == EntityState.Modified) {
// assume we changed the category name and now want to add $1 to all product prices in this category
// stupid i know, but it'll prove a point
Category category = (entityInfo.Entity as Category);
var products = this.Context.Set<Product>().Where(p => p.CategoryID == category.CategoryID);
foreach (var product in products) {
if (!saveMapAdditions.ContainsKey(typeof(Product)))
saveMapAdditions[typeof(Product)] = new List<EntityInfo>();
var ei = this.CreateEntityInfo(product, EntityState.Modified);
ei.ForceUpdate = true;
product.UnitPrice += 1;
// pop product onto list of entities to save
saveMapAdditions[typeof(Product)].Add(ei);
}
}
}
}
}
foreach (var type in saveMapAdditions.Keys) {
if (!saveMap.ContainsKey(type)) {
saveMap[type] = new List<EntityInfo>();
}
foreach (var enInfo in saveMapAdditions[type]) {
saveMap[type].Add(enInfo);
}
}
return saveMap;
}
Controller.cs
C#
1 2 3 4
[HttpPost]
public SaveResult SaveChanges(JObject saveBundle) {
return _contextProvider.SaveChanges(saveBundle);
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.