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

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

问题描述

我对实体框架很新。作为了解更多关于EF的开始,我正在尝试以EF6的一般实现方式,以 http:// genericunitofworkandrepositories为例。 codeplex.com/ 。当我开始创建项目时,我可以尝试简单实现,使用相同的实体保存数据。但是,当我尝试保存数据时,我收到错误。
错误:



  • OriginalValues'(($ ReturnValue1))。OriginalValues'抛出异常的类型
    'System.InvalidOperationException'System.Data.Entity.Infrastructure.DbPropertyValues
    {System.InvalidOperationException}


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



堆栈跟踪:

$ b $在System.Data.Entity.Internal.InternalEntityEntity.ValidateStateToGetValues(String方法,EntityState invalidState)的
$ System.Data.Entity.Internal.InternalEntityEntry中的

  .get_OriginalValues()
在System.Data.Entity.Infrastructure.DbEntityEntry`1.get_OriginalValues()



我有github可用。任何人都可以帮我解决这个问题吗?昨天我被困在这里:)我看到堆栈溢出的类似帖子。但是,他们得到的问题像空值传递的那样没有空值可以在db中被接受。在我的情况下,这不是问题。请检查我的存储库,并建议我可以做什么..任何帮助赞赏。以下是lib链接: https://github.com/tazbir/TryLib



编辑:



错误的地方在这里:

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




条目(实体).State = StateHelper.ConvertState(entity.ObjectState);(执行此行后会触发错误)




 } 



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;

默认值:
返回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天全站免登陆