C# 带有复合键的实体框架一对多关系API
下面是我正在使用的类。我想创建一个从分层到耐心医生的单向参考。如果实体框架需要的话,我也可以建立一对多的关系,但这将始终是1-1,即使这是一个链接器表 我必须为这个ClientId,PatientId,PhysicianId使用一个复合键。我经常犯的错误是C# 带有复合键的实体框架一对多关系API,c#,.net,entity-framework-6,ef-fluent-api,C#,.net,Entity Framework 6,Ef Fluent Api,下面是我正在使用的类。我想创建一个从分层到耐心医生的单向参考。如果实体框架需要的话,我也可以建立一对多的关系,但这将始终是1-1,即使这是一个链接器表 我必须为这个ClientId,PatientId,PhysicianId使用一个复合键。我经常犯的错误是 One or more validation errors were detected during model generation: SPM.Data.Stratification_Physician: : Multiplicity c
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.
什么是正确的fluent api调用
如果可能的话,我真的很想通过PatientClientMedical课程直接到内科医师
谢谢
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 });
转到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);
}
}
公共类PatientClientPhysician配置:
实体配置库
{
公共患者客户端物理配置()
{
ToTable(“患者-医生”);
//组合键:
HasKey(t=>new{t.ClientId,t.PatientId,t.PhysicianId});
属性(m=>m.ClientId).HasColumnName(“ClientId”);
属性(m=>m.PatientId).HasColumnName(“PatientId”);
属性(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.medical).WithMany().HasForeignKey(p=>p.PhysicianId);
}
}
我添加了HasKey,这就是我现在得到的错误。在模型生成过程中检测到一个或多个验证错误:SPM.Data.stratization\u medicine::多重性与关系“stratization\u medicine”中角色“stratization\u medicine\u Target”中的引用约束冲突。由于从属角色中的所有属性都不可为Null,因此主体角色的多重性必须为“1”。这与另一个属性可为Null是不同的问题。这很有效。谢谢
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);
}
}