简体   繁体   English

实体框架一对多关系提供了带有复合键的Fluent API

[英]Entity Framework One to Many Relations ship Fluent API with composite keys

Below are the classes that I am using. 下面是我正在使用的类。 I want to create a one way reference from Stratification to PatientClientPhysician. 我想创建一个从Stratification到PatientClientPhysician的单向引用。 I can also do a one to many relationship if that is required by Entity Framework but this will always be a 1-1 even though this is a linker table. 如果Entity Framework要求,我也可以建立一对多关系,但是即使这是一个链接器表,也始终为1-1。

I have to use a composite key for this ClientId, PatientId, PhysicianId. 我必须为此ClientId,PatientId,PhysicianId使用复合键。 the errors that I keep getting are. 我不断得到的错误是。

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.

What is the correct fluent api calls for this? 为此,正确的流利api调用是什么?

I would really like to go through the PatientClientPhysician class straight to Physician if that is possible. 如果可能的话,我真的很想直接去看PatientClientPhysician类。

Thanks 谢谢

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

edit 1 编辑1

I added 我加了

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

To the PatientClientPhysicianConfiguration class and get this error. 到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'.

This is what I have the relationship set to right now. 这就是我现在设置的关系。

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

Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM