使用 EF6 保存数据失败.错误:OriginalValues 不能用于处于已添加状态的实体 [英] Failed to save data using EF6. Error: OriginalValues cannot be used for entities in the Added state

查看:27
本文介绍了使用 EF6 保存数据失败.错误:OriginalValues 不能用于处于已添加状态的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对实体框架很陌生.作为了解更多关于 EF 的初学者,我正在尝试按照 http://genericunitofworkandrepositories 的示例对 EF6 进行通用实现.codeplex.com/ .当我开始创建项目时,我尝试了简单的实现,并能够使用相同的实体保存数据.但是,现在我在尝试保存数据时遇到了错误.错误:

<块引用>
  • OriginalValues '(($ReturnValue1)).OriginalValues' 引发类型异常'System.InvalidOperationException' System.Data.Entity.Infrastructure.DbPropertyValues{System.InvalidOperationException}

消息是:OriginalValues 不能用于处于已添加状态的实体.

堆栈跟踪:

 在 System.Data.Entity.Internal.InternalEntityEntry.ValidateStateToGetValues(String method, EntityState invalidState)在 System.Data.Entity.Internal.InternalEntityEntry.get_OriginalValues()在 System.Data.Entity.Infrastructure.DbEntityEntry`1.get_OriginalValues()

我在 github 上有它.谁能帮我解决这个问题?我从昨天开始就被困在这里:).我在堆栈溢出上看到了类似的帖子.但是,他们遇到了问题,比如在 db 中不能接受空值的情况下传递了空值.就我而言,这不是问题.请检查我的存储库并建议我能做什么.任何帮助表示赞赏.这是库链接:https://github.com/tazbir/TryLib

出错的地方在这里:

public void SyncObjectState(TEntity entity) where TEntity : class, IObjectState{

<块引用>

Entry(entity).State = StateHelper.ConvertState(entity.ObjectState);(执行此行后触发错误)

<代码> }公共类StateHelper{公共静态实体状态转换状态(对象状态状态){开关(状态){案例对象状态.添加:返回 EntityState.Added;案例 ObjectState.Modified:返回 EntityState.Modified;案例 ObjectState.Deleted:返回 EntityState.Deleted;默认:返回 EntityState.Unchanged;}}}

解决方案

我通过从基类中删除重写方法 SaveChanges 解决了这个问题.虽然我将不得不调查为什么删除 SaveChanges() 方法可以解决问题.

这是我从上下文类中删除以使其工作的代码块.

public override int SaveChanges(){SyncObjectsStatePreCommit();var changes = base.SaveChanges();SyncObjectsStatePostCommit();返回更改;}

谢谢大家...你可能无法理解这个场景.如果有人想玩这个,你可以在 github 上查看我的存储库.

保重

I am quite new to entity framework. As a starter to understand more about EF, I am trying to make a generic implementation of EF6 following example of http://genericunitofworkandrepositories.codeplex.com/ . I was able to save data using same entity while tried plain and simple implementation when I started creating the project. But, now i got error while I tried to save data. The Error:

  • OriginalValues '(($ReturnValue1)).OriginalValues' threw an exception of type 'System.InvalidOperationException' System.Data.Entity.Infrastructure.DbPropertyValues {System.InvalidOperationException}

The message was: OriginalValues cannot be used for entities in the Added state.

Stack trace:

   at System.Data.Entity.Internal.InternalEntityEntry.ValidateStateToGetValues(String method, EntityState invalidState)
   at System.Data.Entity.Internal.InternalEntityEntry.get_OriginalValues()
   at System.Data.Entity.Infrastructure.DbEntityEntry`1.get_OriginalValues()

I have it available on github. Can anyone help me resolve this problem? I am stuck here from yesterday :). I saw similar post on stack overflow. But, they got problem like null value passed where there is no null value can be accepted in db. In my case, that is not the problem. Please check my repository and suggest what i can do.. Any help appreciated. here is the lib link: https://github.com/tazbir/TryLib

Edit:

The place of error is here:

public void SyncObjectState<TEntity>(TEntity entity) where TEntity : class, IObjectState
        {

Entry(entity).State = StateHelper.ConvertState(entity.ObjectState);(error triggers after executing this line)

        }



public class StateHelper
    {
        public static EntityState ConvertState(ObjectState state)
        {
            switch (state)
            {
                case ObjectState.Added:
                    return EntityState.Added;

                case ObjectState.Modified:
                    return EntityState.Modified;

                case ObjectState.Deleted:
                    return EntityState.Deleted;

                default:
                    return EntityState.Unchanged;
            }
        }
    }

解决方案

I resolved the issue by removing overridden method SaveChanges from the base class. Though I will have to investigate why removing SaveChanges() method resolved the issue.

here is the code block that I removed from my context class to get it work.

public override int SaveChanges()
        {
            SyncObjectsStatePreCommit();
            var changes = base.SaveChanges();
            SyncObjectsStatePostCommit();
            return changes;
        }

thanks guys... You might not be able to understand the scenario. If anybody wants to play with this, you can check out my repository at github.

take care

这篇关于使用 EF6 保存数据失败.错误:OriginalValues 不能用于处于已添加状态的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆