簡體   English   中英

實體框架一對多關系提供了帶有復合鍵的Fluent API

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

下面是我正在使用的類。 我想創建一個從Stratification到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 });

嘗試這個:

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