Beispiel #1
0
 /// <summary />
 protected override void Dispose(bool disposing)
 {
     QueryPreCache.RemoveAll(InstanceKey);
     base.Dispose(disposing);
 }
Beispiel #2
0
        /// <summary>
        /// Persists all updates to the data source and resets change tracking in the object context.
        /// </summary>
        /// <returns>The number of objects in an System.Data.Entity.EntityState.Added, System.Data.Entity.EntityState.Modified, or System.Data.Entity.EntityState.Deleted state when System.Data.Objects.ObjectContext.SaveChanges() was called.</returns>
        public override int SaveChanges()
        {
            var cancel = false;

            OnBeforeSaveChanges(ref cancel);
            if (cancel)
            {
                return(0);
            }

            //Get the added list
            var addedList  = this.ObjectContext.ObjectStateManager.GetObjectStateEntries(System.Data.Entity.EntityState.Added);
            var markedTime = System.DateTime.Now;

            //Process added list
            foreach (var item in addedList)
            {
                var entity = item.Entity as IAuditable;
                if (entity != null)
                {
                    var audit = entity as IAuditableSet;
                    if (entity.IsModifyAuditImplemented && entity.ModifiedBy != this.ContextStartup.Modifer)
                    {
                        if (audit != null)
                        {
                            audit.ResetCreatedBy(this.ContextStartup.Modifer);
                        }
                        if (audit != null)
                        {
                            audit.ResetModifiedBy(this.ContextStartup.Modifer);
                        }
                    }
                    audit.CreatedDate  = markedTime;
                    audit.ModifiedDate = markedTime;
                }
            }
            this.OnBeforeSaveAddedEntity(new EventArguments.EntityListEventArgs {
                List = addedList
            });

            //Process modified list
            var modifiedList = this.ObjectContext.ObjectStateManager.GetObjectStateEntries(System.Data.Entity.EntityState.Modified);

            foreach (var item in modifiedList)
            {
                var entity = item.Entity as IAuditable;
                if (entity != null)
                {
                    var audit = entity as IAuditableSet;
                    if (entity.IsModifyAuditImplemented && entity.ModifiedBy != this.ContextStartup.Modifer)
                    {
                        if (audit != null)
                        {
                            audit.ResetModifiedBy(this.ContextStartup.Modifer);
                        }
                    }
                    audit.ModifiedDate = markedTime;
                }
            }
            this.OnBeforeSaveModifiedEntity(new EventArguments.EntityListEventArgs {
                List = modifiedList
            });

            var retval = 0;
            DbContextTransaction customTrans = null;

            try
            {
                _paramList.Clear();
                if (base.Database.CurrentTransaction == null)
                {
                    customTrans = base.Database.BeginTransaction();
                }
                retval += QueryPreCache.ExecuteDeletes(this);
                retval += base.SaveChanges();
                retval += QueryPreCache.ExecuteUpdates(this);
                if (customTrans != null)
                {
                    customTrans.Commit();
                }
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException ex)
            {
                var sb = new System.Text.StringBuilder();
                foreach (var error in ex.EntityValidationErrors)
                {
                    foreach (var validationError in error.ValidationErrors)
                    {
                        sb.AppendLine(validationError.PropertyName + ": " + validationError.ErrorMessage);
                    }
                }
                throw new System.Data.Entity.Validation.DbEntityValidationException(sb.ToString(), ex.EntityValidationErrors);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (customTrans != null)
                {
                    customTrans.Dispose();
                }
            }
            this.OnAfterSaveAddedEntity(new EventArguments.EntityListEventArgs {
                List = addedList
            });
            this.OnAfterSaveModifiedEntity(new EventArguments.EntityListEventArgs {
                List = modifiedList
            });
            OnAfterSaveChanges();
            return(retval);
        }