多重性与参照约束冲突错误 [英] Multiplicity conflicts with the referential constraint Error
问题描述
运行应用程序时出现以下错误:
I get the following error when I run my application:
在模型生成过程中检测到一个或多个验证错误:
One or more validation errors were detected during model generation:
GuideMedApp.Models.NetworkPrescriber_Patients ::多重性
与角色
'NetworkPrescriber_Patients_Source'中的引用约束冲突,关系为
'NetworkPrescriber_Patients'。由于
从属角色中的所有属性都是不可为空的,因此主体角色
的多重性必须为'1'。
GuideMedApp.Models.NetworkPrescriber_Patients: : Multiplicity conflicts with the referential constraint in Role 'NetworkPrescriber_Patients_Source' in relationship 'NetworkPrescriber_Patients'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'.
我正在尝试在实体 NetworkPrescriber和患者之间建立关系。 Patient实体有两个指向NetworkPrescriber表的外键。
I'm trying to create a relationship between entities 'NetworkPrescriber' and 'Patient'. The Patient entity has 2 foreign keys pointing to the NetworkPrescriber table.
以下是定义模型的方式:
Here's how the Models are defined:
患者
[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);
错误表明模型的定义方式与使用fluent API进行的配置方式不匹配但我没看到
如何解决此错误?
The error is suggesting there's a mismatch between how the model are defined and how they're configured withfluent API but I don't see any. How to I fix this error?
推荐答案
尝试添加此配置:
modelBuilder.Entity<Patient>()
.Property(p => p.NetworkPrescriberID).IsOptional();
modelBuilder.Entity<Patient>()
.Property(p => p.SecondaryNetworkPrescriberID).IsOptional();
这篇关于多重性与参照约束冲突错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!