EntityFramework 6 AddOrUpdate不使用复合或复合主键

时间:2022-06-26 19:21:52

The issue has been my weekend nightmare... I have a table where AddOrUpdate is not working correctly, it keeps adding but never updating.

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

All I want to do is when I add a new entity to the table using AddOrUpdate I want it to check the AppointmentId and CompletionCodeId columns and if they match than update, otherwise add.

我想要做的就是当我使用AddOrUpdate向表中添加新实体时,我希望它检查AppointmentId和CompletionCodeId列,如果它们匹配而不是更新,否则添加。

Table structure:

表结构:

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

^^ Not sure if that is even correct.

^^不确定这是否正确。

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

METHOD

方法

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

1 个解决方案

#1


14  

You missed this overload of AddOrUpdate:

您错过了AddOrUpdate的重载:

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

#1


14  

You missed this overload of AddOrUpdate:

您错过了AddOrUpdate的重载:

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