实体框架中高效的集合更新/插入

发布于 2024-10-30 08:18:11 字数 402 浏览 1 评论 0原文

我对这篇文章有类似的挑战:使用实体框架批量插入/更新从几年前开始,我就希望这个故事从那时起就可以改变。

简而言之,我正在运行一个 RESTful 服务,因此我希望 PUT 是面向文档的,并接受一个对象以及子元素的集合。子元素有一个唯一的字符串,我可以用它来确定存在。

与引用的海报不同,我没有查询要求;我想要做的就是能够获取子元素的集合,并在子表上执行插入操作,以查找尚未存在的子元素,并在多对多表上执行插入或删除操作集合的当前状态。理想情况下,具有一定的效率。我意识到我最终可能会将其作为存储过程来执行,我只是想看看是否有一种 EF 原生的方法可以首先工作。

I have a similar challenge to this post: Batch insert/update with entity framework from a couple years ago, I was hoping that the story may have changed since then.

In short, I am running a RESTful service, and as such I'd like a PUT to be document-oriented and take an object along with a collection of child elements in. The child elements have a unique string that I can use for determining existence.

Unlike the referenced poster, I don't have a query requirement; all I want to do is be able to take in a collection of my child elements and perform an insert on the child table for any that aren't already there, and an insert or delete on the many-to-many table to account for the current state of the collection. Ideally, with some efficiency. I realize that I might end up doing this as a sproc, I just wanted to see if there's an EF-native way that works first.

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

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

发布评论

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

评论(1

请止步禁区 2024-11-06 08:18:11

为此,您必须知道哪些项目是新的,或者必须首先查询数据库并将收到的项目与加载的项目合并。 EF 不会为您处理此问题。另请注意,仍然没有批量修改。每个插入、更新或删除都是在数据库的单独往返中执行的。

To do this you must either know which items are new or you must query DB first and merge your received items with loaded items. EF will not handle this for you. Also be aware that there are still no batch modifications. Each insert, update or delete is executed in separate roundtrip to database.

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