2

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);
}
Samuel Slade
  • 8,405
  • 6
  • 33
  • 55
m.edmondson
  • 30,382
  • 27
  • 123
  • 206
  • did you look at the Exception on the link that you posted as far as what could be causing the problem.. are you Closing the Connection anywhere in the Methods that you are calling that you may not be aware of..? – MethodMan Jan 04 '12 at 15:31
  • @DJKRAZE - Yea I read them both however there doesn't seem to be anything that helps me, I don't believe I'm closing the connection as I'm freshly creating the _context prior to calling: `_context = new DataModelContainer();` – m.edmondson Jan 04 '12 at 15:35
  • can you paste that code.. perhaps there is something being done within that new before during or after.. I also noticed for the ID field how is that being set within your DB is that an Auto Gen Field..? – MethodMan Jan 04 '12 at 15:37
  • @DJKRAZE - I've added the extra code that seems relevant to my problem – m.edmondson Jan 04 '12 at 15:42
  • so after this line repo.ExportSiteData(something); repo is not needed for anything..? if so ..it's being disposed of ..just trying to follow your logic.. – MethodMan Jan 04 '12 at 15:44
  • @DJKRAZE - Correct, I'm just testing it at the moment and since all I need to do is update I don't necessarily need any information returned – m.edmondson Jan 04 '12 at 15:46
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/6386/discussion-between-dj-kraze-and-m-edmondson) – MethodMan Jan 04 '12 at 15:51

3 Answers3

2

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

Community
  • 1
  • 1
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
0

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

Carl Prothman
  • 1,461
  • 13
  • 23
0

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.

Alex AIT
  • 17,361
  • 3
  • 36
  • 73