实体框架更新方法
我有几个实体想要同时更新。不过,我想在每个类的部分类文件中为每个实体编写单独的更新方法,并同时调用它们。例如:
public sub UpdateEntity1()
...
end sub
public sub UpdateEntity2()
...
end sub
public sub UpdateEntity3()
...
end sub
public sub UpdateAll()
UpdateEntity1()
UpdateEntity2()
UpdateEntity3()
end sub
我的问题是如何管理对象上下文?我是否在调用 UpdateAll() 的类中创建一个对象上下文,然后将其作为参数传递给每个单独的更新方法?或者我是否为每个更新创建一个新的上下文?我想使用相同的上下文,因为对象是相关的,这会减少更新所有记录的数据库调用。
I have a few entities I'd like to update at the same time. However I'd like to write individual update methods in each classes partial class file, for each entity and call them all at the same time. For example:
public sub UpdateEntity1()
...
end sub
public sub UpdateEntity2()
...
end sub
public sub UpdateEntity3()
...
end sub
public sub UpdateAll()
UpdateEntity1()
UpdateEntity2()
UpdateEntity3()
end sub
My question is how do I manage the object context? do I create one object context in the class I'm calling UpdateAll(), then pass it as a parameter to each individual update method? Or do I create a new context for each Update? I'd like to use the same context because the object are related, and this would decrease the db calls to update all the records.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会按照你说的做,并将上下文作为参数传递。如果对象与其原始上下文分离,您可能需要确保您的更改得到跟踪。
I would do as you said and pass the context as a parameter. You may need to make sure your changes get tracked hough if the object gets detached from its original context.