public SaveResult SaveChanges(JObject saveBundle, TransactionSettings transactionSettings = null) { JsonSerializer = CreateJsonSerializer(); var dynSaveBundle = (dynamic)saveBundle; var entitiesArray = (JArray)dynSaveBundle.entities; var dynSaveOptions = dynSaveBundle.saveOptions; SaveOptions = (SaveOptions) JsonSerializer.Deserialize(new JTokenReader(dynSaveOptions), typeof(SaveOptions)); SaveWorkState = new SaveWorkState(this, entitiesArray); transactionSettings = transactionSettings ?? BreezeConfig.Instance.GetTransactionSettings(); try { if (transactionSettings.TransactionType == TransactionType.TransactionScope) { var txOptions = transactionSettings.ToTransactionOptions(); using (var txScope = new TransactionScope(TransactionScopeOption.Required, txOptions)) { OpenAndSave(SaveWorkState); txScope.Complete(); } } else if (transactionSettings.TransactionType == TransactionType.DbTransaction) { this.OpenDbConnection(); using (IDbTransaction tran = BeginTransaction(transactionSettings.IsolationLevelAs)) { try { OpenAndSave(SaveWorkState); tran.Commit(); } catch { tran.Rollback(); throw; } } } else { OpenAndSave(SaveWorkState); } } catch (EntityErrorsException e) { // SaveWorkState.EntityErrors = e.EntityErrors; var error = new SaveError(e.EntityErrors); var resp = new HttpResponseMessage(e.StatusCode) { Content = new ObjectContent(typeof(SaveError), error, JsonFormatter.Create()), ReasonPhrase = e.Message ?? "Entity Errors exception" }; throw new HttpResponseException(resp); } catch(Exception e2) { if (!HandleSaveException(e2, SaveWorkState)) { throw; } } finally { CloseDbConnection(); } return SaveWorkState.ToSaveResult(); }
/// <summary> /// Perform the actual work of saving the entity data to the database. /// </summary> /// <param name="saveWorkState"></param> protected override void SaveChangesCore(SaveWorkState saveWorkState) { var saveMap = saveWorkState.SaveMap; var conn = _dbConnection; foreach (var type in saveMap.Keys) { List<EntityInfo> entityInfos = saveMap[type]; if (type == typeof(Customer)) { SaveCustomers(conn, entityInfos); } else { throw new Exception("Unable to handle saving type " + type.Name); } } }
public void SaveChanges(Breeze.WebApi.SaveWorkState saveWorkState) { //get your custom saving logic here, for demo purposes this will be very simplistic... EntityInfo info = saveWorkState.EntitiesWithAutoGeneratedKeys.Where(e => e.Entity.GetType() == typeof(ProductDTO)).FirstOrDefault(); var dto = (info.Entity as ProductDTO); //get dbcontext var context = new SummerBreezeDbContext(); //map dto to product context.Products.Add(new Product() { Name = dto.Name, PicUrl = dto.PicUrl, Price = dto.Price, RegisteredDate = DateTime.Now, Tags = dto.Name }); //save changes on the db context.SaveChanges(); // Add new keymappings to breeze (remember we told breeze to generate a temp id, this is the way breeze knows about the new generated id) List <KeyMapping> mappings = new List <KeyMapping>(); mappings.Add(new KeyMapping() { EntityTypeName = dto.GetType().FullName, RealValue = context.Products.Where(p => p.Name == dto.Name).FirstOrDefault().ProductId, TempValue = dto.ProductDTOId }); saveWorkState.KeyMappings = mappings; }
protected abstract void SaveChangesCore(SaveWorkState saveWorkState);
private void OpenAndSave(SaveWorkState saveWorkState) { OpenDbConnection(); // ensure connection is available for BeforeSaveEntities saveWorkState.BeforeSave(); SaveChangesCore(saveWorkState); saveWorkState.AfterSave(); }
// allows subclasses to plug in own save exception handling // either throw an exception here, return false or return true and modify the saveWorkState. protected virtual bool HandleSaveException(Exception e, SaveWorkState saveWorkState) { return false; }
protected override void SaveChangesCore(SaveWorkState saveWorkState) { throw new NotImplementedException(); }
// allows subclasses to plug in own save exception handling // either throw an exception here, return false or return true and modify the saveWorkState. protected virtual bool HandleSaveException(Exception e, SaveWorkState saveWorkState) { return(false); }
protected override void SaveChangesCore(SaveWorkState saveWorkState) { Context.SaveChanges(saveWorkState); }
protected override void SaveChangesCore(SaveWorkState saveWorkState) { var saveMap = saveWorkState.SaveMap; var deletedEntities = ProcessSaves(saveMap); if (deletedEntities.Any()) { ProcessAllDeleted(deletedEntities); } ProcessAutogeneratedKeys(saveWorkState.EntitiesWithAutoGeneratedKeys); int count; try { if (Context is DbContext) { count = ((DbContext)(object)Context).SaveChanges(); } else { count = ObjectContext.SaveChanges(System.Data.Objects.SaveOptions.AcceptAllChangesAfterSave); } } catch (DbEntityValidationException e) { var entityErrors = new List <EntityError>(); foreach (var eve in e.EntityValidationErrors) { var entity = eve.Entry.Entity; var entityTypeName = entity.GetType().FullName; Object[] keyValues; var key = ObjectContext.ObjectStateManager.GetObjectStateEntry(entity).EntityKey; if (key.EntityKeyValues != null) { keyValues = key.EntityKeyValues.Select(km => km.Value).ToArray(); } else { var entityInfo = saveWorkState.EntitiesWithAutoGeneratedKeys.FirstOrDefault(ei => ei.Entity == entity); if (entityInfo != null) { keyValues = new Object[] { entityInfo.AutoGeneratedKey.TempValue }; } else { // how can this happen? keyValues = null; } } foreach (var ve in eve.ValidationErrors) { var entityError = new EntityError() { EntityTypeName = entityTypeName, KeyValues = keyValues, ErrorMessage = ve.ErrorMessage, PropertyName = ve.PropertyName }; entityErrors.Add(entityError); } } saveWorkState.EntityErrors = entityErrors; } catch (Exception) { throw; } saveWorkState.KeyMappings = UpdateAutoGeneratedKeys(saveWorkState.EntitiesWithAutoGeneratedKeys); }
public void SaveChanges(SaveWorkState saveWorkState) { lock (__lock) { _keyMappings.Clear(); var saveMap = saveWorkState.SaveMap; SaveTodoLists(saveMap); SaveTodoItems(saveMap); // ToList effectively copies the _keyMappings so that an incoming SaveChanges call doesn't clear the // keyMappings before the previous version has completed serializing. saveWorkState.KeyMappings = _keyMappings.ToList(); } }