gpt4 book ai didi

c# - 多重性与引用约束错误冲突

转载 作者:行者123 更新时间:2023-12-04 02:02:24 27 4
gpt4 key购买 nike

当我运行我的应用程序时出现以下错误:

One or more validation errors were detected during model generation:

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 实体有 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 配置模型的方式不匹配,但我没有看到任何不匹配。如何修复此错误?

最佳答案

尝试添加这个配置:

modelBuilder.Entity<Patient>()
.Property(p => p.NetworkPrescriberID).IsOptional();
modelBuilder.Entity<Patient>()
.Property(p => p.SecondaryNetworkPrescriberID).IsOptional();

关于c# - 多重性与引用约束错误冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46262476/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com