m.edmondson
m.edmondson

Reputation: 30892

UpdateException: Operation is not valid due to the current state of the object when using Entity Framework and Oracle

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.

Object being created:

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

which is then passed to:

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

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

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

More code:

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(); 
        } 
    }
}

and is called as such:

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

Upvotes: 2

Views: 9345

Answers (3)

Carl Prothman
Carl Prothman

Reputation: 1551

I've been hitting this error while trying to upgrade an older project.

Make sure your project matched the .net framework version listed in your package file. E.g. if you have net452 listed, make sure your project is set to use net 4.5.2. If you have more than one project in your solution, make sure they are all set to the correct .net framework version.

Also, I noticed when adding a new data model to the project, it would get confused as to which model file to add. I believe this was due to the TFS installed on my dev box (I had express, then install latest). To solve this issue, I disconnected the project from TFS for now).

Upvotes: 0

Alex
Alex

Reputation: 18536

I had the same problem over the last couple of days, and it also had to do with primary keys.

In my case, the source database for the model had no primary keys set (for whatever reason).

When generating the model from these tables, the .ssdl looked like this:

<EntitySet Name="..." EntityType="Model.Store...." store:Type="Tables"     store:Schema="..." store:Name="...">
 <DefiningQuery>
 SELECT
 "..."."..." AS "...",
 ...
 </DefiningQuery>
</EntitySet>

"store:Name" and the "DefiningQuery" were not generated when the table already had a primary key.

After removing those two parts, my previously generated .ssdl-files worked, even with tables that still did not have primary keys.

Upvotes: 0

ΩmegaMan
ΩmegaMan

Reputation: 31616

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" />

See

Upvotes: 2

Related Questions