When utilizing Entity Framework with Oracle, an operation is not acceptable owing to the present state of the object.

c# entity-framework exception oracle oracle11g

Question

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);
}
1
2
1/4/2012 4:11:23 PM

Accepted Answer

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

2
5/23/2017 12:30:03 PM

Popular Answer

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).



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow