Entity Framework 更新行,同时排除 IDENTITY 列
Entity Framework update row while excluding IDENTITY column
我正在尝试更新一行,同时忽略数据库中存在的标识列。
更新时出现以下错误:
_context.Entry(existingEvent).Property(x => x.ReferenceNumber).IsModified = false;
_context.Events.Update(existingEvent);
Exception: Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Cannot update identity column 'ReferenceNumber'.
我也尝试了以下但也导致异常:
_context.Events.Update(existingEvent);
_context.Entry(existingEvent.ReferenceNumber).State = EntityState.Unchanged;
System.InvalidOperationException: The entity type 'int' was not found. Ensure that the entity type has been added to the model.
Table:
CREATE TABLE [DW].[Reporting]
(
[ID] UNIQUEIDENTIFIER NOT NULL,
[ReferenceNumber] INT NOT NULL IDENTITY(1,1) UNIQUE,
[Date] DATETIME NOT NULL,
[Address] VARCHAR(256) NOT NULL,
[EventName] VARCHAR(256),
[Subject] VARCHAR(512),
[PhotographerID] UNIQUEIDENTIFIER,
[CameraNumber] VARCHAR(24),
[PictureAmount] INT,
[NegativeID] UNIQUEIDENTIFIER,
[CategoryID] UNIQUEIDENTIFIER,
[People] VARCHAR(256),
CONSTRAINT [PK_Reporting] PRIMARY KEY (ID),
CONSTRAINT [UV_Reporting_Code] UNIQUE ([ReferenceNumber]),
CONSTRAINT [FK_Reporting_Photographers_PhotographerID]
FOREIGN KEY ([PhotographerID]) REFERENCES [DW].[Photographers]([ID]),
CONSTRAINT [FK_Reporting_Negatives_NegativeID]
FOREIGN KEY ([NegativeID]) REFERENCES [DW].[Negatives]([ID]),
CONSTRAINT [FK_Reporting_Categories_CategoryID]
FOREIGN KEY ([CategoryID]) REFERENCES [DW].[Categories]([ID])
)
您刚刚颠倒了代码。 Update()
将所有属性标记为已修改。如果你想排除一个,之后再做。
_context.Events.Update(existingEvent);
_context.Entry(existingEvent).Property(x => x.ReferenceNumber).IsModified = false;
我正在尝试更新一行,同时忽略数据库中存在的标识列。
更新时出现以下错误:
_context.Entry(existingEvent).Property(x => x.ReferenceNumber).IsModified = false;
_context.Events.Update(existingEvent);
Exception: Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Cannot update identity column 'ReferenceNumber'.
我也尝试了以下但也导致异常:
_context.Events.Update(existingEvent);
_context.Entry(existingEvent.ReferenceNumber).State = EntityState.Unchanged;
System.InvalidOperationException: The entity type 'int' was not found. Ensure that the entity type has been added to the model.
Table:
CREATE TABLE [DW].[Reporting]
(
[ID] UNIQUEIDENTIFIER NOT NULL,
[ReferenceNumber] INT NOT NULL IDENTITY(1,1) UNIQUE,
[Date] DATETIME NOT NULL,
[Address] VARCHAR(256) NOT NULL,
[EventName] VARCHAR(256),
[Subject] VARCHAR(512),
[PhotographerID] UNIQUEIDENTIFIER,
[CameraNumber] VARCHAR(24),
[PictureAmount] INT,
[NegativeID] UNIQUEIDENTIFIER,
[CategoryID] UNIQUEIDENTIFIER,
[People] VARCHAR(256),
CONSTRAINT [PK_Reporting] PRIMARY KEY (ID),
CONSTRAINT [UV_Reporting_Code] UNIQUE ([ReferenceNumber]),
CONSTRAINT [FK_Reporting_Photographers_PhotographerID]
FOREIGN KEY ([PhotographerID]) REFERENCES [DW].[Photographers]([ID]),
CONSTRAINT [FK_Reporting_Negatives_NegativeID]
FOREIGN KEY ([NegativeID]) REFERENCES [DW].[Negatives]([ID]),
CONSTRAINT [FK_Reporting_Categories_CategoryID]
FOREIGN KEY ([CategoryID]) REFERENCES [DW].[Categories]([ID])
)
您刚刚颠倒了代码。 Update()
将所有属性标记为已修改。如果你想排除一个,之后再做。
_context.Events.Update(existingEvent);
_context.Entry(existingEvent).Property(x => x.ReferenceNumber).IsModified = false;