When invoking SaveChanges in the.NET Entity Framework, an InvalidOperationException occurs.

.net c# entity-framework

Question

I'm attempting to understand how to utilize the Entity framework, but I've run into a problem that I can't fix. I'm going over a list of the movies I own and entering each one into a straightforward database.

I'm using the following code.

private void AddMovies(DirectoryInfo dir)
{
    MovieEntities db = new MovieEntities();
    foreach (DirectoryInfo d in dir.GetDirectories())
    {
        Movie m = new Movie { Name = d.Name, Path = dir.FullName };
        db.AddToMovies(movie);
    }
    db.SaveChanges();
}

When I execute this, I encounter an error that reads at db.SaveChanges().

The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: AcceptChanges cannot continue because the object's key values conflict with another object in the ObjectStateManager. Make sure that the key values are unique before calling AcceptChanges.

I haven't been able to identify the root of this problem. There are three columns in my database table.
Autoincrement Id int
called nchar (255)
Route nchar (255)

The StoreGeneratedPattern="Identity" setting is present in the SSDL portion of my edmx file, as indicated. I also tried adding ClientAutoGenerated="true" and StoreGenerated="true" to the CSDL as indicated in the blog article. Compile errors (Error 5: The 'ClientAutoGenerated' property is not permitted.) were produced as a consequence of this. The blog entry is from 2006, and it links to a subsequent post, so I believe it has been updated.

However, I am unable to view the follow-up message since it seems to need a Microsoft account.

1
14
6/17/2009 7:04:28 PM

Accepted Answer

I figured out the answer to this.

What occurred was that I neglected to include the main key and set the (Is Identity) parameter to yes when I built my table. Once my entity model was finished, I encountered this issue.

I went back and repaired my database table, but the strange Exception persisted. In the end, deleting the object and then creating it again once the table was corrected was the solution to the issue.

No more deviations

10
6/17/2009 8:28:33 PM

Popular Answer

When I last tested the following code, I declared that everything was in order.

bs.SuspendBinding();
Data.SaveChanges();
bs.ResumeBinding();

The key points I want to make clear to you today are:

1- If we utilize the aforementioned code to suspend binding, we will need to write additional code to address other circumstances, such as index loss in collections and master detail bindings.

2- If we apply the following code in place of the previous code, the exception will disappear and there will be no need to create any more code.

        Data.SaveChanges(System.Data.Objects.SaveOptions.None);

I hope this helps you with your comparable issues.

I'm grateful, pals



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