实体框架一对多关系向Fluent API提供复合键

时间:2015-11-18 20:18:18

标签: c# .net entity-framework-6 ef-fluent-api

以下是我正在使用的课程。我想创建一个从Stratification到PatientClientPhysician的单向引用。如果实体框架需要,我也可以做一对多的关系,但即使这是一个链接表,这也总是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 个答案:

答案 0 :(得分:0)

试试这个:

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