gpt4 book ai didi

c# - EntityFramework 6 AddOrUpdate 不适用于复合或复合主键

转载 作者:可可西里 更新时间:2023-11-01 09:01:09 25 4
gpt4 key购买 nike

这个问题一直是我周末的噩梦...我有一个表,其中 AddOrUpdate 无法正常工作,它一直在添加但从不更新。

我想做的就是当我使用 AddOrUpdate 将新实体添加到表中时,我希望它检查 AppointmentIdCompletionCodeId列,如果匹配则更新,否则添加。

表结构:

CREATE TABLE [dbo].[AppointmentCodes] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[AppointmentId] INT NOT NULL,
[Quantity] INT NOT NULL,
[CompletionCodeId] INT NOT NULL,
CONSTRAINT [PK_AppointmentCodes] PRIMARY KEY CLUSTERED ([Id] ASC, [AppointmentId] ASC));

^^ 不确定这是否正确。

public void AddOrUpdate(T entity)
{
//uses DbContextExtensions to check value of primary key
_context.AddOrUpdate(entity);
Commit();
}

方法

public void AddAppointmentCodes(List<AppointmentCode> appointmentCodes)
{
appointmentCodes.ForEach(x => _appointmentCodeRepository.AddOrUpdate(x));
}

最佳答案

您错过了 this overload AddOrUpdate 的:

_context.AppointmentCodes
.AddOrUpdate(a => new { a.AppointmentId, a.CompletionCodeId },
appointmentCodes.ToArray());

关于c# - EntityFramework 6 AddOrUpdate 不适用于复合或复合主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22287852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com