违反多重约束。关系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

查看:1009
本文介绍了违反多重约束。关系Child_Parent的作用Child_Parent_Target具有多重性1或0..1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了很多研究,但没有找到任何符合我的问题的答案。我甚至试图使用 无法更改关系,因为一个或多个外键属性不可空 示例。没有成功。



我正在使用Entity Framework 6 Code First(Fluent API),POCO Classes。



我有一个叫Parent的课程,一个叫Child的课程



一个父母可以有一个或多个孩子(一对多关系)



所以,在我的ParentMapping类中,我做了:

  HasMany(p => p.Childs)。 WithRequired(p => p.Parent).Map(m => m.MapKey(ParentId)); 

在我的ChildMapping类中,我做了:

  HasRequired(p => p.Parent).WithMany(p => p.Childs).Map(m => m.MapKey(ParentId)); 

我坚持(插入,更新和删除)我的孩子poco类,同时我坚持父类。所以每次保存我的父类时,它的 Childs 属性中有一个或多个子对象。因此,父对象负责保留其子对象。



我按照上面的示例(请参阅链接)将数据保存到数据库中。



当我尝试向db中添加一个新的子节点时,我收到以下消息,父已经在数据库中已经有一个子节点,并且调用了db.SaveChanges :



违反多重约束。 Child_Parent_Target的关系Child_Parent_Target具有多重性1或0..1。



任何人都可以帮我吗?我正在努力,在隧道尽头看不到光。

解决方案

我能够解决我的问题GraphDiff惊人的图书馆。在谈论父母/孩子坚持的时候,这只是为我们做的一切。



在这篇文章中查看: GraphDiff - 允许自动更新分离的实体图表


I did a lot of research but haven't found any answer that matches my problem. I even tried to use the The relationship could not be changed because one or more of the foreign-key properties is non-nullable example. No success.

I'm working with Entity Framework 6 Code First (Fluent API), POCO Classes.

I have a class called Parent and a class called Child

One Parent can have one or more Childs (one to many relationship)

So, in my ParentMapping class I did:

HasMany(p => p.Childs).WithRequired(p => p.Parent).Map(m => m.MapKey("ParentId"));

and in my ChildMapping class I did:

HasRequired(p => p.Parent).WithMany(p => p.Childs).Map(m => m.MapKey("ParentId"));

I persist (Insert, Update and Delete) my child poco class at the same time I persist the parent class. So every time I save my parent class, it has one or more child objects in its Childs property. Thus the parent object is responsible for persist its child objects.

I followed the example above (see the link) to save data into the database.

I get below message when I try to add a new child to the db existent Parent, which already has one child in the database, and call the db.SaveChanges:

Multiplicity constraint violated. The role Child_Parent_Target of the relationship Child_Parent has multiplicity 1 or 0..1.

Can anyone help me? I'm struggling with this and see no light in the end of the tunnel.

解决方案

I was able to solve my problem using GraphDiff amazing library. It just does everything for us when talking about parent/child persisting.

Check it out at this post: GraphDiff - Allowing automated updates of a graph of detached entities

这篇关于违反多重约束。关系Child_Parent的作用Child_Parent_Target具有多重性1或0..1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆