Entity Framework Отношения один ко многим поставляют Fluent API с составными ключами

Ниже приведены классы, которые я использую. Я хочу создать одностороннюю ссылку от стратификации на PatientClientPhysician. Я также могу сделать связь один ко многим, если это требуется Entity Framework, но это всегда будет 1-1, даже если это таблица компоновщика.

Я должен использовать составной ключ для этого ClientId, PatientId, PhysicianId. ошибки, которые я продолжаю получать.

One or more validation errors were detected during model generation:

SPM.Data.Stratification_Physician: : Multiplicity conflicts with the referential constraint in Role 'Stratification_Physician_Target' in relationship 'Stratification_Physician'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'.
Stratification_PrimaryPhysician_Target_Stratification_PrimaryPhysician_Source: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical.
PatientClientPhysician_Stratifications_Target_PatientClientPhysician_Stratifications_Source: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical.

Как правильно говорить свободно API для этого?

Я действительно хотел бы пройти курс PatientClientPhysician прямо к врачу, если это возможно.

Спасибо

public class PatientClientPhysician : BaseEntity
{
    public Guid ClientId { get; set; }
    public Guid PatientId { get; set; }
    public Guid PhysicianId { get; set; }

    public virtual Client Client { get; set; }
    public virtual Patient Patient { get; set; }
    public virtual Physician Physician { get; set; }

    public PatientClientPhysician()
        : base()
    {

    }
}


public class PatientClientPhysicianConfiguration : 
    EntityConfigurationBase<PatientClientPhysician>
{
    public PatientClientPhysicianConfiguration()
    {
        ToTable("patientclientphysician");

        //Property(m => m.Id).HasColumnName("id");
        Property(m => m.ClientId).HasColumnName("clientid");
        Property(m => m.PatientId).HasColumnName("patientid");
        Property(m => m.PhysicianId).HasColumnName("physicianid");

        HasRequired(m => m.Patient).WithMany().HasForeignKey(p => p.PatientId);
        HasRequired(m => m.Client).WithMany().HasForeignKey(c => c.ClientId);
        HasRequired(m => m.Physician).WithMany().HasForeignKey(p => p.PhysicianId);
    }
}

public class Stratification : BaseEntity
{
    public Stratification()
    {
        LabOrders = new List<StratLabOrder>();
        Observations = new List<StratObs>();
        Contacts = new List<StratContact>();
        StratIcd9s = new List<StratIcd9>();
    }

    public Guid ClientLocationId { get; set; }
    public Guid PatientId { get; set; }
    public Guid StratInputId { get; set; }
    public Guid ClientId { get; set; }
    public Guid PhysicianId { get; set; }

    public virtual ClientLocation ClientLocation { get; set; }
    public virtual Patient Patient { get; set; }
    public virtual StratInput StratInput { get; set; }
    public string EpisodeNumber { get; set; }
    public virtual Physician Physician { get; set; }
    public PrintStatus LetterPrintStatus { get; set; }

    public virtual ICollection<StratLabOrder> LabOrders { get; set; }
    public virtual ICollection<StratObs> Observations { get; set; }
    public virtual ICollection<StratContact> Contacts { get; set; }
    public virtual ICollection<StratIcd9> StratIcd9s { get; set; }
    public virtual PatientClientPhysician PrimaryPhysician { get; set; }
}

//this is in EntityConfigurationBase<Stratification> class
HasRequired(m => m.PrimaryPhysician).WithRequired().HasForeignKey(p => new { p.ClientId, p.PatientId, p.PhysicianId });

редактировать 1

я добавил

HasKey(t => new { t.ClientId, t.PatientId, t.PhysicianId });

В класс PatientClientPhysicianConfiguration и получите эту ошибку.

One or more validation errors were detected during model generation:

SPM.Data.Stratification_Physician: : Multiplicity conflicts with the referential constraint in Role 'Stratification_Physician_Target' in relationship 'Stratification_Physician'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'.

Это то, к чему я сейчас отношусь.

HasRequired(m => m.PatientClientPhysicians).WithMany().HasForeignKey(p => new { p.ClientId, p.PatientId, p.PhysicianId });

1 ответ

Решение

Попробуй это:

public class PatientClientPhysicianConfiguration : 
    EntityConfigurationBase<PatientClientPhysician>
{
    public PatientClientPhysicianConfiguration()
    {
        ToTable("patientclientphysician");

        // Composite key:
        HasKey(t => new { t.ClientId, t.PatientId, t.PhysicianId });

        Property(m => m.ClientId).HasColumnName("clientid");
        Property(m => m.PatientId).HasColumnName("patientid");
        Property(m => m.PhysicianId).HasColumnName("physicianid");

        HasRequired(m => m.Patient).WithMany().HasForeignKey(p => p.PatientId);
        HasRequired(m => m.Client).WithMany().HasForeignKey(c => c.ClientId);
        HasRequired(m => m.Physician).WithMany().HasForeignKey(p => p.PhysicianId);
    }
}
Другие вопросы по тегам