adorupdate对复合主键不起作用
本文关键字:不起作用 复合 adorupdate | 更新日期: 2023-09-27 17:49:14
这个问题一直是我周末的噩梦…我有一个表,其中AddOrUpdate
不能正常工作,它不断添加,但从不更新。
我想做的就是当我使用AddOrUpdate
向表添加一个新实体时,我希望它检查AppointmentId
和CompletionCodeId
列,如果它们匹配,则更新,否则添加。
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));
}
您错过了AddOrUpdate
的重载:
_context.AppointmentCodes
.AddOrUpdate(a => new { a.AppointmentId, a.CompletionCodeId },
appointmentCodes.ToArray());