WCF 和 EF 多对多获取重复项

发布于 2024-12-04 11:28:46 字数 696 浏览 3 评论 0原文

在此处输入图像描述

使用上面的模型,我尝试保存一个新的 PamNewsMessage 并添加为其添加标签,然后将其保存到数据库中。然而,在尝试了添加对象和保存的许多不同组合之后,我仍然遇到重复条目(不是重复键)的相同问题。

在此处输入图像描述

目前,这是我用来进行更改的内容,它适用于 PamNewsMessage,但是如果标签已经存在,则会产生重复项。

_theService = new WCFPamUpdateServiceEntities3();
_theService.PamNewsMessages.AddObject(pnm);
_theService.SaveChanges();

我不确定我还需要在这里做什么,它应该非常简单。我读过很多人有类似的问题,但我没有找到明确的解决方案。任何人都可以帮助我吗?我已经在这方面搞砸了好几个小时了,现在正在阅读并尝试不同的方法组合来保存事物、改变状态等等。

谢谢,

理查德

很抱歉无法发布图片,长期读者第一次发帖:-)
(为您解决了这个问题 - marc_s)

enter image description here

Using the model above, I am trying to save a new PamNewsMessage and add Tags to it then save it to the DB. However, after trying many different combinations of adding objects and saving, I still have the same problem with duplicate entries (not duplicate keys).

enter image description here

Currently, this is what I am using to make the changes, and it works for the PamNewsMessage, however if a Tag already exists, it makes a duplicates.

_theService = new WCFPamUpdateServiceEntities3();
_theService.PamNewsMessages.AddObject(pnm);
_theService.SaveChanges();

I am not sure what else I need to do here, it should be pretty straight forward. I have read a lot of people having similar problems with no clear fix that I have found. Can anybody help me out here I have messed around with this for many hours now reading and trying different combinations of ways to save things, changing states, and what not.

Thanks,

Richard

Sorry about not being able to post pictures, long time reader first time poster :-)
(fixed this for you - marc_s)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黎歌 2024-12-11 11:28:46

这里是我第一次遇到这个问题时的一些理论。 这里是一些相关的描述EF。

简短的回答是:EF 不会为你做这件事。

您使用了分离的对象,现在您必须明确告诉 EF 您做了哪些更改 = 这通常意味着处理图中每个实体和每个 图中的独立关联(多对多始终是独立的 协会)。如果您调用 AddObject,您将告诉 EF 添加整个对象图,而不仅仅是单个实体。

如果您只知道要添加 NewsMessage 并且此消息将仅包含现有标签,您可以执行以下操作:

_theService = new WCFPamUpdateServiceEntities3();
_theService.PamNewsMessages.AddObject(pnm);
foreach (var tag in pnm.Tags)
{
    _theService.ObjectStateManager.ChangeObjectState(tag, EntityState.Unchanged);
}
_theService.SaveChanges();

显然,一旦您进入更复杂的场景,您还可以添加新标签或删除与现有标签的连接,这个简单的解决方案将不起作用。

Here is little bit theory when I met the problem first time. Here is some description related to EF.

Short answer is: EF will not do this for you.

You worked with detached objects and now you must explicitly tell EF what changes you did = it generally means playing with state of every entity in the graph and every independent association in the graph (many-to-many is always independent association). If you call AddObject you are telling EF to add whole object graph not only the single entity.

If you just know that you are adding NewsMessage and this message will have only existing Tags you can do something like:

_theService = new WCFPamUpdateServiceEntities3();
_theService.PamNewsMessages.AddObject(pnm);
foreach (var tag in pnm.Tags)
{
    _theService.ObjectStateManager.ChangeObjectState(tag, EntityState.Unchanged);
}
_theService.SaveChanges();

Obviously once you go to more complicated scenarios where you can also add new tags or remove connections to existing tags this simple solution will not work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文