Brad Overstreet
Brad Overstreet

Reputation: 75

The changes to the database were committed successfully, but an error occurred while updating the object context

I have a winforms application that uses EF5, .NET 4, and SQL Server 2008. I have the following table\entity in the database. The T-SQL to create the tables is shown below.

CREATE TABLE [Admin].[StandardTerms](
    [StdTermID] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](50) NOT NULL,
    [Description] [nvarchar](100) NOT NULL,
 CONSTRAINT [PK_StandardTerms] PRIMARY KEY CLUSTERED 
(
    [StdTermID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

I use a BindingSource object on the form which is linked to a data grid that allows the users to create new standard terms. When a user creates a new standard term and tries to save it, I get the following error.

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.

The primary key for the standard term table is an automatically generated integer. I have checked the SSDL and made sure that the StoreGeneratedPattern is defined on the entity.

<EntityType Name="StandardTerms">
    <Key>
        <PropertyRef Name="StdTermID" />
    </Key>
    <Property Name="StdTermID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
    <Property Name="Name" Type="nvarchar" Nullable="false" MaxLength="50" />
    <Property Name="Description" Type="nvarchar" Nullable="false" MaxLength="100" />
</EntityType>

The strange thing is that this only happens when I create a standard term from the grid. I can use my repository to create one and it works just fine. I can even create the exact same binding source structure and it works without the error. At this point I am stumped. Does anyone have any ideas on what might be going on here?

Upvotes: 4

Views: 12219

Answers (4)

BurnsBA
BurnsBA

Reputation: 4939

I had a trigger and sequence defined, but forgot to set StoreGeneratedPattern to Identity.

Upvotes: 0

I got this error when I forgot to mark one of the multi-column-primary-key fields as [Key]

Upvotes: 1

user2771188
user2771188

Reputation:

Another one option yielding such error message is INSTEAD OF trigger adding a resultset with no Id column on it. E.g.: https://i.sstatic.net/3GXUw.png

And Entity Framework by requesting the following will not get Id column.

insert [dbo].[Xxxx]([XxxxId], [FirstName], [LastName], [ProfileUrl], 
        [LastModified], [PictureUrl], [Headline])
values (@0, @1, @2, null, @3, null, null)
select [Id], [Revision]
from [dbo].[Xxxx]
where @@ROWCOUNT > 0 and [Id] = scope_identity()

Fix was to add Id column into the RS.

Upvotes: 0

Brad Overstreet
Brad Overstreet

Reputation: 75

I finally found my problem and as I suspected it was something stupid I did. In my model, I was building the query, loading values and returning a BindingList. What I did to fix it was to check to see if my BindingList was null, if it was I built the query, loaded the values and created the Binding List.

Original Code

public BindingList<StandardTerm> StandardTerms
{
    get
    {
        _uow.StandardTerms.FindAll().Load();
        _standardTerms = _uow.StandardTerms.GetBindingList();
    }
}

Correct Code

public BindingList<StandardTerm> StandardTerms
{
    get
    {
        if (_standardTerms == null)
        {
            _uow.StandardTerms.FindAll().Load();
            _standardTerms = _uow.StandardTerms.GetBindingList();
        }

        return _standardTerms;
    }
}

My repository class implements the method GetBindingList which is shown below.

protected DbSet<T> _set;
public BindingList<T> GetBindingList()
{
    return _set.Local.ToBindingList();
}

My entities all rely on the ObservableListSource which is described in the following MDSN article.

EF DataBinding with WinForms

Upvotes: 2

Related Questions