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

2

我终于找到了我的问题,因为我怀疑这是我做的愚蠢的事情。在我的模型中,我正在构建查询、加载值并返回一个 BindingList。我所做的修复它是检查我的 BindingList 是否为空,如果它是我构建查询,加载值并创建绑定列表。

原始代码

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

正确的代码

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

        return _standardTerms;
    }
}

我的存储库类实现了 GetBindingList 方法,如下所示。

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

我的实体都依赖于以下 MDSN 文章中描述的 ObservableListSource。

EF 数据绑定与 WinForms

于 2013-06-05T16:34:29.167 回答
1

当我忘记将多列主键字段之一标记为时出现此错误[Key]

于 2015-04-04T18:25:58.267 回答
0

产生此类错误消息的另一个选项是 INSTEAD OF 触发器添加一个没有 Id 列的结果集。例如:http: //i.stack.imgur.com/3GXUw.png

而Entity Framework通过以下请求不会得到Id列。

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

修复是将 Id 列添加到 RS 中。

于 2013-11-12T22:08:04.203 回答
0

我定义了触发器和序列,但忘记设置StoreGeneratedPatternIdentity.

于 2018-11-27T21:03:05.470 回答