实体框架中高效的集合更新/插入
我对这篇文章有类似的挑战:使用实体框架批量插入/更新从几年前开始,我就希望这个故事从那时起就可以改变。
简而言之,我正在运行一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为此,您必须知道哪些项目是新的,或者必须首先查询数据库并将收到的项目与加载的项目合并。 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.