UpdateException:由于使用Entity Framework和Oracle时对象的当前状态,操作无效 [英] UpdateException: Operation is not valid due to the current state of the object when using Entity Framework and Oracle

查看:845
本文介绍了UpdateException:由于使用Entity Framework和Oracle时对象的当前状态,操作无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图添加一个新的实体对象的持久性,但是我得到一个 UpdateException 谁的内在异常是 InvalidOperationException 带有消息:

I'm attempting to add a new Entity object for persistence, however I'm getting an UpdateException who's inner exception is an InvalidOperationException with the message:


由于对象的当前状态,操作无效。

Operation is not valid due to the current state of the object.

正在创建的对象:

var something = new SITE
{
    EntityKey = new EntityKey("DataModelContainer.SITE", "SITE_ID", "ID"),
                    SITE_COMMON_REFERENCE = "hello",
                    SITE_ID = "hello"     
};

然后传递给:

public void ExportSiteData (SITE exportSiteData)
{
    _context.SITE.AddObject(exportSiteData);
    _context.SaveChanges(); //<-- Exception here
}

数据库是Oracle 11g,我可以成功地通过实体框架提取数据。

The database is Oracle 11g and I can succesfully extract data via Entity Framework.

我猜这个问题是更多的数据库端,但是我可以使用没有键/引用的hello值成功填充它完整性问题。

I'm guessing the problem is more database side, however I can successfully populate it with both "hello" values with no key/referential integrity problems.

我非常欣赏指向正确的方向。谢谢

I'd much appreciate being pointed in the right direction. Thanks

更多代码

ExportSiteData位于 BillRepository 实现 IDisposable

ExportSiteData is within BillRepository which implements IDisposable:

class BillRepository : IDisposable
{
    private readonly DataModelContainer _context;        

    public BillRepository()
    {
        _context = new DataModelContainer();
    }

    public void ExportSiteData (SITE exportSiteData)
    {
        _context.SITE.AddObject(exportSiteData);
        _context.SaveChanges(); //<-- Exception here
    }

    public void Dispose()
    {
        if (_context != null) 
        { 
            _context.Dispose(); 
        } 
    }
}

并被调用:

using (var repo = new BillRepository())
{
    repo.ExportSiteData(something);
}


推荐答案

验证主键(在xml中拉起.edmx文件),表中有两个部分都有StoreGeneratedPattern =Identity。有一个已知的错误,EF通过设计器修改一个部分,但不修改其他部分。在两个部分中查找:

Verify that the primary key (pull up the .edmx file in xml) for the table in question has StoreGeneratedPattern="Identity" in both sections. There is a known error where EF via the designer modifies one section, but doesn't modify the other section. Look for this in both sections:

<EntityType Name="TIMELINE">
          <Key>
            <PropertyRef Name="ID"/>
          </Key>

  <Property Name="ID" Type="decimal" Nullable="false" Precision="19" StoreGeneratedPattern="Identity" />

查看

  • EF4: Bug in StoreGeneratedPattern SSDL
  • ObjectContext.SaveChanges() violates primary key, throws UpdateException?

这篇关于UpdateException:由于使用Entity Framework和Oracle时对象的当前状态,操作无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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