首页 新闻 赞助 找找看

EF Entry.OringalValues不能获得原始值

0
悬赏园豆:10 [已解决问题] 解决于 2014-08-08 09:16

 对一个要进行Update的Entity,在SaveChange之前,使用DbContext的Entry方法获得Entry对象,再获得OringalValues集合(包括Clone),再读取值时,都是最新的值,而不是更新之前的值。

而之前在另外一个地方是OK的,请问这个是什么原因?

        public EntityLogInfoImpl(EntityLogActionType logAction, TEntity entity, IDbContext dbContext = null, string logActionName = null)
            : base(logAction, entity, logActionName)
        {
            _dbContext = dbContext;
            LogInfo = EntityLogPropertyInfos(entity);
            if(logAction == EntityLogActionType.Update)
            {
                var entry = (_dbContext as DbContext).Entry(entity);
                this.OriginalValues = entry.OriginalValues.Clone();
            }
        }

 

        private IEntityLogInfo<T> CreateEntityLogInfo(EntityLogActionType logAction, T entity, bool autoUpdate = true, string logActionName = null)
        {
            try
            {
                IEntityLogInfo<T> result;
                if (_createEmptyEntityLog)
                {
                    result = EntityLogInfo<T>.CreateEmptyEntityLogInfo(entity);
                }
                else
                {
                    result = new EntityLogInfoImpl<T>(logAction, entity, this._context, logActionName);// EntityLogInfo<T>.CreateDefaultEntityLogInfo(logAction, entity);
                }
                if (autoUpdate && _autoUpdateEntityLog)
                {
                    result.Update();
                }
                return result;
            }
            catch
            {
                return EntityLogInfo<T>.CreateEmptyEntityLogInfo(entity);
            }
        }

 

        public IEntityLogInfo<T> Update(T entity, bool autoLog = true, string logAction = null)
        {
            if (entity == null)
                throw new ArgumentNullException("entity");
            var logInfo = CreateEntityLogInfo(EntityLogActionType.Update, entity, false, logAction);
            if (this.AutoCommitEnabled)
            {
                _context.SaveChanges();
            }
            else
            {
                try
                {
                    this.Entities.Attach(entity);
                    InternalContext.Entry(entity).State = System.Data.Entity.EntityState.Modified;
                }
                finally { }
            }
            if (autoLog && this._autoUpdateEntityLog)
            {
                logInfo.Update();
            }
            return logInfo;
        }

以上是在IRepository里实现,而之前的操作是直接在Service里实现,在调用IRepository的Update之前处理。

519740105的主页 519740105 | 大侠五级 | 园豆:5810
提问于:2014-08-07 18:40
< >
分享
最佳答案
1

期间调用了别的数据更新处理,在执行CONTEXT.SUBMITCHANGES时,都同步更新了。

519740105 | 大侠五级 |园豆:5810 | 2014-08-08 09:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册