【问题标题】:Multiplicity is not valid in Role 'xxx' in relationship 'xxx'... the upper bound of the multiplicity of the Dependent Role must be '1'多重性在关系“xxx”中的角色“xxx”中无效...从属角色多重性的上限必须为“1”
【发布时间】:2016-06-04 17:52:29
【问题描述】:

我有以下型号:

    public class Annonce
    {
        [Key]
        public int annonceID { get; set; }


        [DisplayName("User")]
        public int userID { get; set; }
        public virtual User users { get; set; }


        [DisplayName("Sous type")]
        public int sous_type_annonceID { get; set; }
        public virtual Sous_type_annonce sous_type_annonces { get; set; }


        [DisplayName("Gouvernorat")]
        public int gouvernoratID { get; set; }
        public virtual Gouvernorat gouvernorats { get; set; }
    }

我正在尝试在同一个实体中定义 3 一对多的关系

这是我的 DbContext:

        modelBuilder.Entity<Annonce>().HasRequired(p => p.gouvernorats)
            .WithMany(b => b.Annonces).HasForeignKey(b => b.annonceID);

        modelBuilder.Entity<Annonce>().HasRequired(p => p.sous_type_annonces)
            .WithMany(b => b.Annonces).HasForeignKey(b => b.sous_type_annonceID);

        modelBuilder.Entity<Annonce>().HasRequired(p => p.users)
            .WithMany(b => b.Annonces).HasForeignKey(b => b.sous_type_annonceID);

Gouvernorat、User 和 Sous_type_annonce 都有这个 ICollection:

public virtual ICollection<Annonce> Annonces { get; set; }

我收到验证错误

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

Annonce_gouvernorats_Source: : 多重性在角色中无效 关系“Annonce_gouvernorats”中的“Annonce_gouvernorats_Source”。 因为 Dependent Role 指的是关键属性,所以上 从属角色的多重性的界限必须是“1”。

出了什么问题,我该如何解决?

【问题讨论】:

    标签: c# asp.net asp.net-mvc


    【解决方案1】:

    我解决了问题

    modelBuilder.Entity<Annonce>().HasRequired(p => p.gouvernorats)
       .WithMany(b => b.Annonces).HasForeignKey(b => b.gouvernoratID);
    

    使用“gouvnoratID”而不是“annonceID”

    modelBuilder.Entity<Annonce>().HasRequired(p => p.gouvernorats)
                .WithMany(b => b.Annonces).HasForeignKey(b => b.annonceID);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多