【问题标题】:Multiplicity conflicts with the referential constraint Error多重性与引用约束冲突错误
【发布时间】:2017-09-17 09:19:47
【问题描述】:

我在运行我的应用程序时收到以下错误:

在模型生成过程中检测到一个或多个验证错误:

GuideMedApp.Models.NetworkPrescriber_Patients: : 多重性 与 Role 中的引用约束冲突 关系中的“NetworkPrescriber_Patients_Source” 'NetworkPrescriber_Patients'。因为所有的属性在 依赖角色是不可为空的,主要角色的多重性 必须是“1”。

我正在尝试在实体“NetworkPrescriber”和“Patient”之间建立关系。 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);

该错误表明模型的定义方式与使用fluent API 配置的方式之间存在不匹配,但我没有看到任何内容。 如何修复此错误?

【问题讨论】:

  • 能否显示所有配置?
  • @MegaTron 这是我对这两个实体的唯一配置
  • 尝试为模型添加完整配置
  • 我也没有发现您所展示的内容有任何问题。并且无法繁殖。这里肯定有其他没有显示的东西。

标签: c# sql-server entity-framework


【解决方案1】:

尝试添加此配置:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多