简体   繁体   中英

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. 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.

I have to use a composite key for this 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?

I would really like to go through the PatientClientPhysician class straight to Physician if that is possible.

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

I added

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

To the PatientClientPhysicianConfiguration class and get this error.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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