public override int SaveChanges(System.Data.Objects.SaveOptions options)
    {
        var generator = new IdGenerator();

        foreach (var entry in this.ObjectStateManager.GetObjectStateEntries(EntityState.Added))
        {
            var entity = entry.Entity as IEntity;
            if (entity != null)
            {
                entity.Id = generator.CreateNewId();
            }
        }
        return(base.SaveChanges(options));
    }
 public override int SaveChanges(System.Data.Objects.SaveOptions options)
 {
     if (EnforceReadonlyBehavior)
     {
         if (
             ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Added).Any()
             ||
             ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Modified).Any()
             ||
             ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Deleted).Any()
             )
         {
             throw new InvalidOperationException("nope");
         }
     }
     return(base.SaveChanges(options));
 }
Exemplo n.º 3
0
        /// <summary>
        /// Saves the changes inside the unit of work.
        /// </summary>
        /// <param name="saveOptions">The save options.</param>
        /// <exception cref="InvalidOperationException">A transaction is running. Call CommitTransaction instead.</exception>
        public virtual void SaveChanges(SaveOptions saveOptions = SaveOptions.DetectChangesBeforeSave | SaveOptions.AcceptAllChangesAfterSave)
        {
            Log.Debug("Saving changes | {0}", Tag);

            if (IsInTransaction)
            {
                const string error = "A transaction is running. Call CommitTransaction instead.";

                Log.Error(error);
                throw new InvalidOperationException(error);
            }

            var objectContext = DbContext.GetObjectContext();

            objectContext.SaveChanges(saveOptions);

            Log.Debug("Saved changes | {0}", Tag);
        }
    public override int SaveChanges(System.Data.Objects.SaveOptions options)
    {
        var modified = this.ObjectStateManager.GetObjectStateEntries(EntityState.Modified | EntityState.Added); // Get the list of things to update
        var result   = base.SaveChanges(options);                                                               // Call the base SaveChanges, which clears that list.

        using (var context = new WebDataContext())                                                              // This is the second database context.
        {
            foreach (var obj in modified)
            {
                var table = obj.Entity as IAdvantageWebTable;
                if (table != null)
                {
                    table.UpdateWeb(context);                             // This is IAdvantageWebTable.UpdateWeb(), which calls all the existing logic I've had in place for years.
                }
            }
            context.SubmitChanges();
        }
        return(result);
    }
Exemplo n.º 5
0
        public override int SaveChanges(System.Data.Objects.SaveOptions options)
        {
            // checking changed elements to see if there is something wrong
            foreach (var objectStateEntry in this.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified))
            {
                var obj = objectStateEntry.Entity;
                if (obj != null)
                {
                    // checking DateTime properties (they must all be UTC)
                    if (DebugConfig.IsDebug)
                    {
                        var changedProperties = objectStateEntry.GetModifiedProperties();

                        foreach (var changedProperty in changedProperties)
                        {
                            var newValue = objectStateEntry.CurrentValues[changedProperty];

                            if (newValue is DateTime)
                            {
                                var value = (DateTime)newValue;
                                if (value.Kind != DateTimeKind.Utc)
                                {
                                    throw new Exception("Invalid value for 'DateTime' property: " + changedProperty + ". Must be UTC.");
                                }
                                if (value.Year < 1800)
                                {
                                    throw new Exception("Invalid value for 'DateTime' property: " + changedProperty + ". Must be greater than year 1800.");
                                }
                            }
                        }
                    }
                }
            }

            return(base.SaveChanges(options));
        }
Exemplo n.º 6
0
 public void SaveChange(System.Data.Objects.SaveOptions saveOptions)
 {
     throw new NotImplementedException();
 }
 public int SaveChanges(System.Data.Objects.SaveOptions options)
 {
     return(_dataContext.SaveChanges(options));
 }
Exemplo n.º 8
0
 public override int SaveChanges(System.Data.Objects.SaveOptions options)
 {
     //update your objects here
     return(base.SaveChanges(options));
 }
Exemplo n.º 9
0
        public override int SaveChanges(System.Data.Objects.SaveOptions options)
        {
            DetectChanges();

            #region Creation
            var addedEntities = ObjectStateManager.GetObjectStateEntries(EntityState.Added);

            foreach (var addEntity in addedEntities)
            {
                if (addEntity != null)
                {
                    if (addEntity.Entity != null)
                    {
                        PropertyInfo auditProperty = addEntity.Entity.GetType().GetProperty("AuditLog");
                        if (auditProperty != null)
                        {
                            AuditLog auditLog = (AuditLog)addEntity.Entity.GetType().GetProperty("AuditLog").GetValue(addEntity.Entity, null);
                            auditLog.CreatedOn = System.DateTime.Now;
                            auditLog.CreatedBy = Session.UserId;
                            addEntity.Entity.GetType().GetProperty("AuditLog").SetValue(addEntity.Entity, auditLog, null);
                        }
                    }
                }
            }
            #endregion

            #region Modification

            var modifiedEntities = ObjectStateManager.GetObjectStateEntries(EntityState.Modified);

            foreach (var modifiedentity in modifiedEntities)
            {
                if (modifiedentity != null)
                {
                    if (modifiedentity.Entity != null)
                    {
                        PropertyInfo auditProperty = modifiedentity.Entity.GetType().GetProperty("AuditLog");
                        if (auditProperty != null)
                        {
                            AuditLog auditLog = (AuditLog)modifiedentity.Entity.GetType().GetProperty("AuditLog").GetValue(modifiedentity.Entity, null);
                            auditLog.ModifiedOn = System.DateTime.Now;
                            auditLog.ModifiedBy = Session.UserId;
                            modifiedentity.Entity.GetType().GetProperty("AuditLog").SetValue(modifiedentity.Entity, auditLog, null);
                        }
                    }
                }
            }
            #endregion

            #region Deletion
            var deletedEntities = ObjectStateManager.GetObjectStateEntries(EntityState.Deleted);
            foreach (var deleteEntity in deletedEntities)
            {
                if (deleteEntity != null)
                {
                    if (deleteEntity.Entity != null)
                    {
                        deleteEntity.ChangeState(EntityState.Modified);
                        PropertyInfo auditProperty = deleteEntity.Entity.GetType().GetProperty("AuditLog");
                        if (auditProperty != null)
                        {
                            AuditLog auditLog = (AuditLog)deleteEntity.Entity.GetType().GetProperty("AuditLog").GetValue(deleteEntity.Entity, null);
                            auditLog.ModifiedOn = System.DateTime.Now;
                            auditLog.ModifiedBy = Session.UserId;
                            deleteEntity.Entity.GetType().GetProperty("AuditLog").SetValue(deleteEntity.Entity, auditLog, null);
                        }
                        PropertyInfo statusProperty = deleteEntity.Entity.GetType().GetProperty("Status");
                        if (statusProperty != null)
                        {
                            Status status = (Status)deleteEntity.Entity.GetType().GetProperty("Status").GetValue(deleteEntity.Entity, null);
                            status.IsDeleted = true;
                        }
                    }
                }
            }
            #endregion

            return(base.SaveChanges(options));
        }