【问题标题】:EF Multiplicity constraint violated. The role ... of the relationship ... has multiplicity 1 or 0..1违反了 EF 多重性约束。关系...的角色...具有多重性 1 或 0..1
【发布时间】:2016-05-27 18:27:45
【问题描述】:

我收到了错误

Multiplicity constraint violated. The role 'BegrotingsCategorie_children_Source' of the relationship 'NET.DAL.EF.BegrotingsCategorie_children' has multiplicity 1 or 0..1.

尝试从我的数据库中读取“BegrotingsCategorie”时。 这段代码触发了异常

 public BegrotingsCategorie ReadBegrotingsCategorie(int begrotingsCategorieId)
        {
            return _ctx.Begrotingscategorieen.Find(begrotingsCategorieId);
        }

我的 BegrotingsCategorie 模型如下所示

 [Table("tblBegrotingsCategorieen")]
    public class BegrotingsCategorie
    {
        [Key]
        public int BegrotingsCategorieId { get; set; }
        public string Informatie { get; set; }
        public double Uitgaven { get; set; }
        public Begroting Begroting { get; set; }
        public BegrotingsCategorie Parent { get; set; }
        public double Percentage { get; set; } 
        public double BerekendLoon { get; set; }

        public virtual ICollection<BegrotingsCategorie> children { get; set; }
        public virtual ICollection<Actie> Acties { get; set; }

    }

我不知道如何解决这个问题..

编辑 数据库中的一些行

database

第一行是父行,所以它的 Parent_BegrotingsCategorieId 为 NULL, 它有 1 个 Parent_BegrotingsCategorieId 1 的孩子,他们自己也有多个孩子,所以他们有 Parent_BegrotingsCategorieId 2

我像这样添加我的数据

public BegrotingsCategorie AddBegrotingsCategorie(string informatie, Begroting begroting, BegrotingsCategorie parentCategorie)
        {
            BegrotingsCategorie begrotingsCategorie = new BegrotingsCategorie()
            {
                Informatie = informatie,
                Begroting = begroting,
                Parent = parentCategorie
            };
            return _repo.CreateBegrotingsCategorie(begrotingsCategorie);
        }

【问题讨论】:

  • 您能否展示 BegrotingsCategorie 类和这些类的 Fluent API 映射?请记住,它必须对 Category 类具有 FK 才能建立一对多关系。
  • begrotingsCategorie 类如上所示,'children' 列表有来自同一类的 Parent。如果我在孩子上方添加 [ForeignKey("Parent")],我会收到此错误 The ForeignKeyAttribute on property 'children' on type 'NET.BL.Domain.Begroting.BegrotingsCategorie' is not valid. The foreign key name 'Parent' was not found on the dependent type 'NET.BL.Domain.Begroting.BegrotingsCategorie'. The Name value should be a comma separated list of foreign key property names.
  • 您需要一个 int 类型的 Id,您可以在其中添加属性 ForeignKey。这允许您的 DBMS 使用 ForeignKey=primary key 约束自行加入您的表。
  • 嗯,但奇怪的是它直到几个小时前才起作用。而且我的数据库中有 3500 行数据,所以添加新的 ForeignKey 属性并不容易。。
  • 你到底做了什么改变?如果没有定义外键,这种关系可能永远不会起作用。

标签: c# mysql asp.net-mvc entity-framework


【解决方案1】:

我认为您的 ICollection&lt;BegrotingsCategorie&gt; children 属性在您的 BegrotingsCategorie 课程中给您带来了噪音。在代码中的某处定义了 BegrotingsCategories 的一对一或零对一递归关系。但是,您正试图通过将您的属性 children 声明为 ICollection 在您的类中使用 BegrotingsCategories 的一对多关系。如果您真的不需要它,您可能希望在您的BegrotingsCategorieclass 中删除您的children 属性。这可能是一种可能的解决方法。

【讨论】:

  • 这不是问题的核心。因为没有定义 FK,EF 使用 PK=FK,因此找到了一对一的关系,但是使用 ICollection 定义了一对多,这就是引发此错误的原因。
  • @downvoter,你能告诉我们你为什么这样做,以便我们都能学到新东西吗?
  • @DevilSuichiro 我不知道 OP 的意图是什么,真正需要 children 属性,因为他之前已经定义了一个或零对一的约束。我邀请您发布您的 cmets 作为答案,让 OP 决定哪个答案更适合他的需求。
猜你喜欢
  • 2013-03-30
  • 1970-01-01
  • 1970-01-01
  • 2014-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多