当前位置: 动力学知识库 > 问答 > 编程问答 >

linq - Nullable part of primary key causes ChangeConflictException: Row not found or changed

问题描述:

It appears that Linq-to-sql fails to generate proper code for updating a single column when the primary key consists of at least one nullable column. Is this correct, and is there a way around it?

For a table with the following composite primary key,

<Column Name="FK_CompanyId" Type="System.Int32" DbType="Int NOT NULL"

IsPrimaryKey="true" CanBeNull="false" />

<Column Name="FK_RegistrationId" Type="System.Int32" DbType="Int NULL"

IsPrimaryKey="true" CanBeNull="true" />

<Column Name="IsSnapshot" Type="System.Boolean" DbType="Bit NOT NULL"

IsPrimaryKey="true" CanBeNull="false" />

consisting of two non-nullable columns and a nullable column, when trying to update a single row,

OrmFoo ormFoo = db.Foos.Single(foo => foo.FK_CompanyId == companyId);

ormFoo.Bar = (byte)bar;

db.Commit();

the following problem occurs,

ChangeConflictException: Row not found or changed.

Inspecting the generated SQL,

UPDATE [Database].[Foos]

SET [Bar] = @p19

WHERE ([FK_CompanyId] = @p0)

AND ([FK_RegistrationId] = @p1)

AND (NOT ([IsSnapshot] = 1))

AND ...

But in this concrete case, FK_RegistrationId is NULL, so the correct code it generates should be

 AND ([FK_RegistrationId] IS NULL

Is there a way to keep the nullable component of the primary key and still have the optimistic concurrency check generate correct code?

分享给朋友:
您可能感兴趣的文章:
随机阅读: