简体   繁体   English

违反了EF多重性约束。 关系的角色…具有多重性1或0..1

[英]EF Multiplicity constraint violated. The role … of the relationship … has multiplicity 1 or 0..1

I am getting the error 我收到错误

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

when trying to read a 'BegrotingsCategorie' from my database. 尝试从我的数据库中读取“ BegrotingsCategorie”时。 This piece of code triggers the exception 这段代码触发异常

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

My BegrotingsCategorie model looks like this 我的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; }

    }

I have no idea how to solve this.. 我不知道如何解决这个问题。

Edit Some rows in the database 编辑数据库中的某些行

database 数据库

The first row is the parent so its Parent_BegrotingsCategorieId is NULL, it has 1 child with Parent_BegrotingsCategorieId 1 who on its own also has multiple children so they have Parent_BegrotingsCategorieId 2 第一行是父级,因此其Parent_BegrotingsCategorieId为NULL,它有1个孩子,其Parent_BegrotingsCategorieId 1本身也有多个孩子,因此他们有Parent_BegrotingsCategorieId 2

I add my data like this 我这样添加我的数据

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

I think your ICollection<BegrotingsCategorie> children property is giving you noise in your BegrotingsCategorie class. 我认为您的ICollection<BegrotingsCategorie> children属性在您的BegrotingsCategorie类中给您带来噪音。 There is a one or zero-to-one recursive relationship of BegrotingsCategorie s defined somewhere in your code. 在代码中的某个BegrotingsCategorie定义了BegrotingsCategorie的一对一或零对一的递归关系。 However, you are trying to use a one-to-many relationship of BegrotingsCategorie s in your class by declaring your property children as an ICollection . 但是,您尝试通过将您的属性children级声明为ICollection在类中使用BegrotingsCategorie的一对多关系。 You may want to get rid of your children property in your BegrotingsCategorie class if you really do not need it. 如果您确实不需要,可以在BegrotingsCategorie类中摆脱其children属性。 This may be a possible workaround. 这可能是一种解决方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 违反多重性约束。 角色 &#39; <T> 关系的&#39;具有多重性1或0..1 - Multiplicity constraint violated. The role '<T>' of the relationship has multiplicity 1 or 0..1 EF6 数据库优先:违反多重性约束。 关系“...”的角色“...”具有多重性 1 或 0..1 - EF6 Database-First: Multiplicity constraint violated. The role '…' of the relationship '…' has multiplicity 1 or 0..1 违反多重性约束。 关系Child_Parent的角色Child_Parent_Target具有多重性1或0..1 - Multiplicity constraint violated. The role Child_Parent_Target of the relationship Child_Parent has multiplicity 1 or 0..1 违反多重性约束。 关系“ LibarySystem.DataModel”的角色“ Student_LendedBooks…”具有多重性1或0..1 - Multiplicity constraint violated. The role 'Student_LendedBooks…' of the relationship 'LibarySystem.DataModel' has multiplicity 1 or 0..1 违反多重性约束。 关系xxx的角色xxx具有多重性1或0..1 - Multiplicity constraint violated. The role xxx of the relationship xxx has multiplicity 1 or 0..1 &#39;违反了多重约束。 关系 Rayon_Produits&#39; 的角色 &#39;Rayon_Produits_Source&#39; 具有多重性 1 或 0..1。 - 'Multiplicity constraint violated. The role 'Rayon_Produits_Source' of the relationship Rayon_Produits' has multiplicity 1 or 0..1.' 违反了多重约束。 “Repository.Person_Projects”关系的“Person_Projects_Source”角色具有多重性1或0..1 - Multiplicity constraint violated. The role 'Person_Projects_Source' of the relationship 'Repository.Person_Projects' has multiplicity 1 or 0..1 EF 6.0代码优先:违反多重性约束。 关系的作用 - EF 6.0 Code First: Multiplicity constraint violated. the role of the relationship 主要角色的多重性必须为0..1 - Multiplicity of the Principal Role must be 0..1 违反多重性约束的实体框架6 - Multiplicity constraint violated Entity framework 6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM