C# EntityFramework 6 AddOrUpdate无法使用复合主键或复合主键

C# EntityFramework 6 AddOrUpdate无法使用复合主键或复合主键,c#,sql,sql-server,entity-framework,foreign-keys,C#,Sql,Sql Server,Entity Framework,Foreign Keys,这个问题一直是我周末的噩梦。。。我有一个表,AddOrUpdate工作不正常,它不断添加,但从不更新 我只想在使用AddOrUpdate向表中添加新实体时检查AppointmentId和CompletionCodeId列,如果它们匹配,则添加 表结构: CREATE TABLE [dbo].[AppointmentCodes] ( [Id] INT IDENTITY (1, 1) NOT NULL, [AppointmentId] INT NOT

这个问题一直是我周末的噩梦。。。我有一个表,
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));
}
public void addappointcodes(列出appointcodes)
{
AppointCodes.ForEach(x=>_AppointCodeRepository.AddOrUpdate(x));
}
您错过了添加或更新的

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

如果这些属性中的任何一个都是可为空的整数,那么它将不起作用——或者如果有人能告诉我如何使用,那么它可能会起作用。如果您使用的是基本实体类字段,那么它也不起作用