예제 #1
0
        public override int SaveChanges(System.Data.Objects.SaveOptions options)
        {
            foreach (ObjectStateEntry entry in ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified))
            {
                dynamic e = entry.Entity;

                if (entry.State == EntityState.Added)
                {
                    try
                    {
                        e.CreatedDate = DateTime.Now;
                        e.CreatedBy   = OAMSSetting.Username;
                    }
                    catch (Exception)
                    {
                    }
                }

                try
                {
                    e.LastUpdatedDate = DateTime.Now;
                    e.LastUpdatedBy   = OAMSSetting.Username;
                }
                catch (Exception)
                {
                }



                // Validate the objects in the Added and Modified state
                // if the validation fails throw an exeption.
            }
            return(base.SaveChanges(options));
        }
예제 #2
0
        public override int SaveChanges(System.Data.Objects.SaveOptions options)
        {
            List <ObjectStateEntry> entries = this.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Added | System.Data.EntityState.Deleted | System.Data.EntityState.Modified).ToList();

            foreach (ObjectStateEntry e in entries)
            {
                SetCreateChange(e);
                //RaiseEvents(e);
            }

            return(base.SaveChanges(options));
        }
예제 #3
0
 public new int SaveChanges(System.Data.Objects.SaveOptions options)
 {
     return(base.SaveChanges(options));
 }
예제 #4
0
        public override int SaveChanges(System.Data.Objects.SaveOptions options)
        {
            if (AppSetting.Offline)
            {
            }
            else
            {
                foreach (ObjectStateEntry entry in ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified))
                {
                    dynamic e = entry.Entity;

                    if (entry.State == EntityState.Added)
                    {
                        try
                        {
                            e.CreatedDate = DateTime.Now;
                            e.CreatedBy   = OAMSSetting.Username;
                        }
                        catch (Exception)
                        {
                        }

                        if (e is Site)
                        {
                            var site = e as Site;
                            site.UpdateScore();
                        }
                    }

                    if (entry.State == EntityState.Modified)
                    {
                        int count = entry.CountPropertiesChanged();

                        if (e is Site)
                        {
                            if (count > AppSetting.PropertiesCount)
                            {
                                throw new Exception("Site is modified with many properties.");
                            }

                            var site = e as Site;
                            site.UpdateScore();
                        }

                        if (count == 0)
                        {
                            this.ObjectStateManager.ChangeObjectState(e, EntityState.Unchanged);
                        }
                        else
                        {
                            try
                            {
                                e.LastUpdatedDate = DateTime.Now;
                                e.LastUpdatedBy   = OAMSSetting.Username;
                            }
                            catch (Exception)
                            {
                            }
                        }
                    }

                    // Validate the objects in the Added and Modified state
                    // if the validation fails throw an exeption.
                }
            }
            return(base.SaveChanges(options));
        }
예제 #5
0
 public virtual async Task SaveChangesAsync(SaveOptions saveOptions)
 {
     await SaveChangesAsync();
 }
예제 #6
0
 public virtual void SaveChanges(SaveOptions saveOptions)
 {
     SaveChanges();
 }
        public override int SaveChanges(System.Data.Objects.SaveOptions options)
        {
            //detect changes
            if (options.HasFlag(SaveOptions.DetectChangesBeforeSave))
            {
                this.DetectChanges();
            }

            if (this.ConetxtUser != null)
            {
                foreach (var entry in ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Modified | System.Data.EntityState.Added))
                {
                    System.Reflection.PropertyInfo prop = null;
                    if (!entry.IsRelationship && entry.Entity != null)
                    {
                        prop = entry.Entity.GetType().GetProperty("UpdatedAt");
                        if (prop != null)
                        {
                            prop.SetValue(entry.Entity, DateTime.Now, null);
                        }

                        prop = entry.Entity.GetType().GetProperty("UpdatedById") ?? entry.Entity.GetType().GetProperty("UpdatedBy");
                        if (prop != null)
                        {
                            prop.SetValue(entry.Entity, this.ConetxtUser.Id, null);
                        }
                    }
                }
            }

            foreach (var entry in ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Modified | System.Data.EntityState.Added | System.Data.EntityState.Deleted))
            {
                if (entry.Entity is EmergencyCap)
                {
                    var ec = entry.Entity as EmergencyCap;
                    foreach (var name in entry.GetModifiedProperties())
                    {
                        if (new string[] { "updatedat", "updatedby" }.Contains(name.ToLower()))
                        {
                            continue;
                        }

                        var h = new History()
                        {
                            FieldName = name,
                            OldValue  = string.Concat(new object[] { entry.OriginalValues[name] }),
                            NewValue  = string.Concat(new object[] { entry.CurrentValues[name] }),

                            UpdatedBy   = ec.UpdatedBy,
                            UpdateDate  = ec.UpdatedAt,
                            ReferenceId = ec.Id,
                            TableName   = entry.EntitySet.Name
                        };
                        this.Histories.AddObject(h);
                    }
                }
            }

            if (this.ConetxtUser != null)
            {
                //call PropertyChanging event trigger on objects that support it before sending field updates to the db
                var clientsUpdating = ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Modified);
                var permissions     = CC.Data.Services.PermissionsFactory.GetPermissionsFor(this.ConetxtUser);
                foreach (var entry in clientsUpdating)
                {
                    foreach (var propertyName in entry.GetModifiedProperties())
                    {
                        var propChangingNotifier = entry.Entity as INotifyPropertySaving;
                        if (propChangingNotifier != null)
                        {
                            ValidationContext validationContext = new ValidationContext(entry, null, new Dictionary <object, object>()
                            {
                                { typeof(User), this.ConetxtUser }
                            });

                            try
                            {
                                //notifiy an object of a property update will be set to a database
                                propChangingNotifier.OnPropertySaving(propertyName, permissions);
                            }
                            catch (PropertyChangeDeniedException)
                            {
                                //the property change is invalid
                            }
                        }
                    }
                }
            }

            try
            {
                return(base.SaveChanges(options));
            }
            catch
            {
                throw;
            }
        }