Question

When I adding more than one consecutive data an error occurred in SaveChanges() method.

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

My baseservice

public void Delete(T entity)
    {
        ObjectSet.DeleteObject(entity);
        Context.SaveChanges();
    }

    public void Add(T entity)
    {
        ObjectSet.AddObject(entity);
        Context.SaveChanges();

    }

    public void Attach(T entity)
    {
        ObjectSet.Attach(entity);
        Context.SaveChanges();
    }

    public void Update(Expression<Func<T, bool>> where, T entity)
    {
        var ent = First(where);
        ent = entity;
        Context.SaveChanges();
    }
Was it helpful?

Solution

Are you certain you're adding different entities to EF? The inner exception states that AcceptChanges() is failing because the current entity that you're trying to add shares a key with an entity that's already being tracked.

For more on AcceptChanges(), take a look at: http://msdn.microsoft.com/en-us/library/system.data.objects.objectstateentry.acceptchanges.aspx

OTHER TIPS

I had this problem and found out that I was doing the following operations, making EntityFramework to become not in sync with the data in the database:

1) Make a query on the rows of a table through Entity Framework's Context. Doing so, EntityFramework context preserves a copy of those objects in its Local view.

2) Truncate the table through an SQL Query (so the Entity Framework context has not idea this happened. The entities are still in its local view even if they were truncated in the database). Since the primary key of the table is auto incrementing (IDENTITY (1,1)), the truncation call makes the primary key counter of the table to reset to 1.

3) Add rows to the table through Entity Framework, and then call SaveChanges(). Because of the table truncation, the primary key of the new row is 1. After creating the row, EntityFramework queries for the database for the row values, creates a new Entity, populates the values in the Entity and adds the Entity to its local view.

Because the context already had another object with primary key = 1 stored in its local view (from Step 1), an exception is thrown as it tries to add a second Entity with the same primary key to the local view.

To avoid this situation, Entity Framework must be remain in sync with the database content before making new operations.

In my case, I had to fix this by calling:

Context.MyTableEntities.Local.Clear();
Context.SaveChanges();

So the Entities were deleted first, and the context was told about it. Then I truncated the table with an SQL Query to reset the auto increment counter.

Since the objects were deleted from the local view first, and the table truncation done afterwards (so it resets the auto increment counter), primary key conflicts were avoided.

Hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top