Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EntityFramework 6 AddOrUpdate not working with compound or composite primary key

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

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.

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));
}
like image 633
devfunkd Avatar asked Mar 09 '14 20:03

devfunkd


1 Answers

You missed this overload of AddOrUpdate:

_context.AppointmentCodes
        .AddOrUpdate(a => new { a.AppointmentId, a.CompletionCodeId },
                     appointmentCodes.ToArray());
like image 67
Gert Arnold Avatar answered Sep 20 '22 15:09

Gert Arnold