[英]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 编辑数据库中的某些行
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.