Exemplo n.º 1
0
        public virtual void UpdateWithHistory(T entity)
        {
            try
            {
                if (entity.GetType() == typeof(Device))
                {
                    Device edited = (Device)(object)entity;
                    iotRepository <DeviceParameter> repo    = new iotRepository <DeviceParameter>();
                    iotRepository <Device>          devrepo = new iotRepository <Device>();
                    Device devbefore = devrepo.GetById(edited.Id);

                    foreach (var item in edited.Actions)
                    {
                        foreach (var param in item.ResultParameters)
                        {
                            DeviceParameter stparam = repo.GetById(param.Id);
                            if (stparam != null)
                            {
                                if (!stparam.Value.Equals(param.Value))
                                {
                                    ActionChangeHistory hist = new ActionChangeHistory();
                                    hist.Date     = DateTime.Now;
                                    hist.Property = param;
                                    hist.Value    = param.Value;
                                    iotRepository <ActionChangeHistory> histrepo = new iotRepository <ActionChangeHistory>();
                                    histrepo.Add(hist);
                                }
                            }
                        }
                    }


                    foreach (var item in edited.Properties)
                    {
                        foreach (var param in item.ResultParameters)
                        {
                            DeviceParameter stparam = repo.GetById(param.Id);
                            if (stparam != null)
                            {
                                if (!stparam.Value.Equals(param.Value))
                                {
                                    ParameterChangeHistory hist = new ParameterChangeHistory();
                                    hist.Date     = DateTime.Now;
                                    hist.Property = param;
                                    hist.Value    = param.Value;
                                    iotRepository <ParameterChangeHistory> histrepo = new iotRepository <ParameterChangeHistory>();
                                    histrepo.Add(hist);
                                }
                            }
                        }
                    }
                }
                else if (entity.GetType() == typeof(DeviceAction))
                {
                    iotRepository <DeviceParameter> repo = new iotRepository <DeviceParameter>();
                    DeviceAction item = (DeviceAction)(object)entity;
                    foreach (var param in item.ResultParameters)
                    {
                        DeviceParameter stparam = repo.GetById(param.Id);
                        if (stparam != null)
                        {
                            if (!stparam.Value.Equals(param.Value))
                            {
                                ActionChangeHistory hist = new ActionChangeHistory();
                                hist.Date     = DateTime.Now;
                                hist.Property = param;
                                hist.Value    = param.Value;
                                iotRepository <ActionChangeHistory> histrepo = new iotRepository <ActionChangeHistory>();
                                histrepo.Add(hist);
                            }
                        }
                    }
                }
                else if (entity.GetType() == typeof(DeviceProperty))
                {
                    iotRepository <DeviceParameter> repo = new iotRepository <DeviceParameter>();
                    DeviceProperty item = (DeviceProperty)(object)entity;

                    foreach (var param in item.ResultParameters)
                    {
                        DeviceParameter stparam = repo.GetById(param.Id);
                        if (stparam != null)
                        {
                            if (!stparam.Value.Equals(param.Value))
                            {
                                ParameterChangeHistory hist = new ParameterChangeHistory();
                                hist.Date     = DateTime.Now;
                                hist.Property = param;
                                hist.Value    = param.Value;
                                iotRepository <ParameterChangeHistory> histrepo = new iotRepository <ParameterChangeHistory>();
                                histrepo.Add(hist);
                            }
                        }
                    }
                }

                DbEntityEntry dbEntityEntry = iotGenericGlobalContext <T> .DbContext.Entry(entity);

                if (dbEntityEntry.State == EntityState.Detached)
                {
                    iotGenericGlobalContext <T> .DbSet.Attach(entity);
                }
                dbEntityEntry.State = EntityState.Modified;
                //iotGenericGlobalContext<T>.DbContext.SaveChanges();
            }
            catch (Exception exc)
            {
                throw;
            }
        }
Exemplo n.º 2
0
        public virtual void UpdateWithHistory(T entity)
        {
            try
            {
                if (entity.GetType() == typeof(Device))
                {
                    Device edited = (Device)(object)entity;
                    iotRepository <DeviceParameter> repo    = new iotRepository <DeviceParameter>();
                    iotRepository <Device>          devrepo = new iotRepository <Device>();
                    Device devbefore = devrepo.GetById(edited.Id);

                    foreach (var item in edited.Actions)
                    {
                        foreach (var param in item.ResultParameters)
                        {
                            DeviceParameter stparam = repo.GetById(param.Id);
                            if (stparam != null)
                            {
                                if (!stparam.Value.Equals(param.Value))
                                {
                                    ActionChangeHistory hist = new ActionChangeHistory();
                                    hist.Date     = DateTime.Now;
                                    hist.Property = param;
                                    hist.Value    = param.Value;
                                    iotRepository <ActionChangeHistory> histrepo = new iotRepository <ActionChangeHistory>();
                                    histrepo.Add(hist);
                                }
                            }
                        }
                    }


                    foreach (var item in edited.Properties)
                    {
                        foreach (var param in item.ResultParameters)
                        {
                            DeviceParameter stparam = repo.GetById(param.Id);
                            if (stparam != null)
                            {
                                if (!stparam.Value.Equals(param.Value))
                                {
                                    ParameterChangeHistory hist = new ParameterChangeHistory();
                                    hist.Date     = DateTime.Now;
                                    hist.Property = param;
                                    hist.Value    = param.Value;
                                    iotRepository <ParameterChangeHistory> histrepo = new iotRepository <ParameterChangeHistory>();
                                    histrepo.Add(hist);
                                }
                            }
                        }
                    }
                }
                else if (entity.GetType() == typeof(DeviceAction))
                {
                    iotRepository <DeviceParameter> repo = new iotRepository <DeviceParameter>();
                    DeviceAction item = (DeviceAction)(object)entity;
                    foreach (var param in item.ResultParameters)
                    {
                        DeviceParameter stparam = repo.GetById(param.Id);
                        if (stparam != null)
                        {
                            if (!stparam.Value.Equals(param.Value))
                            {
                                ActionChangeHistory hist = new ActionChangeHistory();
                                hist.Date     = DateTime.Now;
                                hist.Property = param;
                                hist.Value    = param.Value;
                                iotRepository <ActionChangeHistory> histrepo = new iotRepository <ActionChangeHistory>();
                                histrepo.Add(hist);
                            }
                        }
                    }
                }
                else if (entity.GetType() == typeof(DeviceProperty))
                {
                    iotRepository <DeviceParameter> repo = new iotRepository <DeviceParameter>();
                    DeviceProperty item = (DeviceProperty)(object)entity;

                    foreach (var param in item.ResultParameters)
                    {
                        DeviceParameter stparam = repo.GetById(param.Id);
                        if (stparam != null)
                        {
                            if (!stparam.Value.Equals(param.Value))
                            {
                                ParameterChangeHistory hist = new ParameterChangeHistory();
                                hist.Date     = DateTime.Now;
                                hist.Property = param;
                                hist.Value    = param.Value;
                                iotRepository <ParameterChangeHistory> histrepo = new iotRepository <ParameterChangeHistory>();
                                histrepo.Add(hist);
                            }
                        }
                    }
                }

                DbSet.Attach(entity);
            }
            catch (Exception e)
            {
                _logger.Error(e, e.Message);
            }
        }
Exemplo n.º 3
0
        public override int SaveChanges()
        {
            ChangeTracker.DetectChanges(); // Important!

            ObjectContext ctx = ((IObjectContextAdapter)this).ObjectContext;

            List <ObjectStateEntry> objectStateEntryList =
                ctx.ObjectStateManager.GetObjectStateEntries(EntityState.Added
                                                             | EntityState.Modified
                                                             | EntityState.Deleted)
                .ToList();

            foreach (ObjectStateEntry entry in objectStateEntryList)
            {
                if (!entry.IsRelationship)
                {
                    switch (entry.State)
                    {
                    case EntityState.Added:
                        // write log...
                        break;

                    case EntityState.Deleted:
                        // write log...
                        break;

                    case EntityState.Modified:
                    {
                        if (ObjectContext.GetObjectType(entry.Entity.GetType()) == typeof(DeviceParameter))
                        {
                            DbDataRecord original = entry.OriginalValues;
                            string       oldValue = original.GetValue(
                                original.GetOrdinal("Value"))
                                                    .ToString();

                            CurrentValueRecord current  = entry.CurrentValues;
                            string             newValue = current.GetValue(
                                current.GetOrdinal("Value"))
                                                          .ToString();

                            if (oldValue != newValue)     // probably not necessary
                            {
                                ParameterChangeHistory hist = new ParameterChangeHistory();
                                hist.Date     = DateTime.Now;
                                hist.Property = (DeviceParameter)(object)entry.Entity;
                                hist.Value    = newValue;
                                this.ParameterChanges.Add(hist);
                            }

                            if (this.ParamUpdateEvent != null)
                            {
                                Task.Factory.StartNew(() => this.ParamUpdateEvent((DeviceParameter)entry.Entity));
                            }
                        }
                        else if (ObjectContext.GetObjectType(entry.Entity.GetType()) == typeof(DeviceActionResult))
                        {
                            DbDataRecord original = entry.OriginalValues;
                            string       oldValue = original.GetValue(
                                original.GetOrdinal("Value"))
                                                    .ToString();

                            CurrentValueRecord current  = entry.CurrentValues;
                            string             newValue = current.GetValue(
                                current.GetOrdinal("Value"))
                                                          .ToString();

                            if (oldValue != newValue)     // probably not necessary
                            {
                                ActionChangeHistory hist = new ActionChangeHistory();
                                hist.Date     = DateTime.Now;
                                hist.Property = (DeviceActionResult)(object)entry.Entity;
                                hist.Value    = newValue;
                                this.ActionChangeHistory.Add(hist);
                            }

                            if (this.ActionUpdateEvent != null)
                            {
                                Task.Factory.StartNew(() => this.ActionUpdateEvent((DeviceActionResult)entry.Entity));
                            }
                        }
                        else if (ObjectContext.GetObjectType(entry.Entity.GetType()) == typeof(Device))
                        {
                            Task.Factory.StartNew(() => this.DeviceUpdateEvent((Device)entry.Entity));
                        }
                        break;
                    }
                    }
                }
            }
            return(base.SaveChanges());
        }