4

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?

4

4 に答える 4

1

複数列の主キー フィールドの 1 つを次のようにマークするのを忘れたときに、このエラーが発生しました。[Key]

于 2015-04-04T18:25:58.267 に答える
0

StoreGeneratedPatternトリガーとシーケンスを定義しましたが、に設定するのを忘れていましたIdentity

于 2018-11-27T21:03:05.470 に答える