运行我的应用程序时出现以下错误:
在模型生成期间检测到一个或多个验证错误:
GuideMedApp.Models.NetworkPrescriber_Patients ::多重性 与角色中的引用约束冲突 ' NetworkPrescriber_Patients_Source'谈恋爱 ' NetworkPrescriber_Patients&#39 ;.因为所有的属性 依赖角色是不可为空的,主要角色的多样性 必须是' 1'。
我试图在实体和NetworkPrescriber'之间建立关系。和''患者'。 Patient实体有2个指向NetworkPrescriber表的外键。
以下是模型的定义方式:
病人
[Table("Patient")]
public partial class Patient
{
public long PatientID { get; set; }
public long? NetworkPrescriberID { get; set; }
public long? SecondaryNetworkPrescriberID { get; set; }
public virtual NetworkPrescriber NetworkPrescriber { get; set; }
public virtual NetworkPrescriber NetworkPrescriber1 { get; set; }
}
NetworkPrescriber
[Table("NetworkPrescriber")]
public partial class NetworkPrescriber
{
public NetworkPrescriber()
{
Patients = new HashSet<Patient>();
Patients1 = new HashSet<Patient>();
}
public long NetworkPrescriberID { get; set; }
public virtual ICollection<Patient> Patients { get; set; }
public virtual ICollection<Patient> Patients1 { get; set; }
}
模型配置如下:
modelBuilder.Entity<NetworkPrescriber>()
.HasMany(e => e.Patients)
.WithOptional(e => e.NetworkPrescriber)
.HasForeignKey(e => e.NetworkPrescriberID);
modelBuilder.Entity<NetworkPrescriber>()
.HasMany(e => e.Patients1)
.WithOptional(e => e.NetworkPrescriber1)
.HasForeignKey(e => e.SecondaryNetworkPrescriberID);
错误表明模型的定义方式与如何使用流畅的API进行配置之间存在不匹配但我看不到任何错误。 如何修复此错误?
答案 0 :(得分:1)
尝试添加此配置:
modelBuilder.Entity<Patient>()
.Property(p => p.NetworkPrescriberID).IsOptional();
modelBuilder.Entity<Patient>()
.Property(p => p.SecondaryNetworkPrescriberID).IsOptional();