private Dictionary <Type, List <EntityInfo> > ThrowEntityErrorsException(Dictionary <Type, List <EntityInfo> > saveMap) { List <EntityInfo> orderInfos; if (saveMap.TryGetValue(typeof(Order), out orderInfos)) { var errors = orderInfos.Select(oi => { #if NHIBERNATE return(new EntityError() { EntityTypeName = typeof(Order).FullName, ErrorMessage = "Cannot save orders with this save method", ErrorName = "WrongMethod", KeyValues = new object[] { ((Order)oi.Entity).OrderID }, PropertyName = "OrderID" }); #else return(new EFEntityError(oi, "WrongMethod", "Cannot save orders with this save method", "OrderID")); #endif }); var ex = new EntityErrorsException("test of custom exception message", errors); // if you want to see a different error status code use this. // ex.StatusCode = HttpStatusCode.Conflict; // Conflict = 409 ; default is Forbidden (403). throw ex; } return(saveMap); }
public SaveResult SaveWithEntityErrorsException([FromBody] SaveBundle saveBundle) { return(SaveChangesCore(saveBundle, configurator => configurator.BeforeApplyChanges( context => { var saveMap = context.SaveMap; if (saveMap.TryGetValue(typeof(Order), out var orderInfos)) { var errors = orderInfos.Select(oi => { return new EntityError { EntityTypeName = typeof(Order).FullName, ErrorMessage = "Cannot save orders with this save method", ErrorName = "WrongMethod", KeyValues = new object[] { ((Order)oi.ClientEntity).OrderID }, PropertyName = "OrderID" }; }); var ex = new EntityErrorsException("test of custom exception message", errors); // if you want to see a different error status code use this. // ex.StatusCode = HttpStatusCode.Conflict; // Conflict = 409 ; default is Forbidden (403). throw ex; } }))); }