[英]How often do I need to save the context?
In a repository, I am doing these two consecutive tasks: 在存储库中,我正在执行以下两个连续任务:
// Update vendorOrder
vendorOrder.VendorOrderStatus = VendorOrderStatus.Completed;
vendorOrderRepository.UpdateVendorOrder(vendorOrder);
vendorOrderRepository.Save();
// Update order
order.OrderStatus = OrderStatus.Completed;
orderRepository.UpdateOrder(order);
orderRepository.Save();
Both vendorOrderRepository
and orderRepositoryrepositorty
have their own Save()
method: vendorOrderRepository
和orderRepositoryrepositorty
都有自己的Save()
方法:
public void Save()
{
context.SaveChanges();
}
Will calling Save()
inside each repository save only changes to the context there, or will it save each and every change made to context in any other repositories up to that point? 在每个存储库中调用
Save()
会只保存对上下文的更改,还是将对上下文所做的每个更改都保存到该存储库中?
In my example, is it redundant to call Save()
twice? 在我的示例中,两次调用
Save()
是否多余? Will it work if I just call Save()
at the very end? 如果我最后只调用
Save()
,它将起作用吗?
(I could just try myself and see what works, but I want to have a definite answer based on how MVC works if possible, in case there are exceptions. It's probably basic understanding of MVC, but I skipped that part...) (我可以自己尝试一下,看看有什么用,但是如果可能的话,我想根据可能的MVC工作原理给出一个明确的答案。这可能是对MVC的基本理解,但是我跳过了这一部分...)
vendorOrderRepository begins with: vendorOrderRepository以以下内容开头:
public class VendorOrderRepository : IVendorOrderRepository, IDisposable
{
private ApplicationDbContext context = new ApplicationDbContext();
private IOrderRepository orderRepository;
public VendorOrderRepository(ApplicationDbContext context)
{
this.context = context;
orderRepository = new OrderRepository(context);
}
orderRepository begins with: orderRepository以以下内容开头:
public class OrderRepository : IOrderRepository, IDisposable
{
private ApplicationDbContext context = new ApplicationDbContext();
private IVendorOrderRepository vendorOrderRepository;
public OrderRepository(ApplicationDbContext context)
{
this.context = context;
vendorOrderRepository = new VendorOrderRepository(context);
}
How often do I need to save the context?
我需要多久保存一次上下文?
As often as it makes sense to do so. 这样做通常是有意义的。 In general, you will want to only call SaveChanges once, unless dealing with large sets in which case it will make sense to use transactions and save in batches.
通常,您只需要调用一次SaveChanges,除非处理大型集,在这种情况下,使用事务并批量保存是有意义的。
Will calling Save() inside each repository save only changes to the context there, or will it save each and every change made to context in any other repositories up to that point?
在每个存储库中调用Save()会只保存对上下文的更改,还是将对上下文所做的每个更改都保存到该存储库中?
"the context there" is kind of vague. “那里的上下文”有点模糊。 Calling SaveChanges will save all changes made inside of the context.
调用SaveChanges将保存在上下文中进行的所有更改。 So, if both
vendorOrderRepository
and orderRepository
share the same context, then calling it once will save every change made. 因此,如果
vendorOrderRepository
和orderRepository
共享相同的上下文,则对其进行一次调用将保存所做的所有更改。
In my example, is it redundant to call Save() twice?
在我的示例中,两次调用Save()是否多余? Will it work if I just call Save() at the very end?
如果我最后只调用Save(),它将起作用吗?
If they use the same context, then yes. 如果他们使用相同的上下文,则为是。 If different, then it is not redundant.
如果不同,则不是多余的。
In a broader sense, the way that SaveChanges works is that it will save the entities stored in the change tracker (See ChangeTracker Class MDN for expanded details). 从广义上讲,SaveChanges的工作方式是将保存存储在变更跟踪器中的实体(有关扩展的详细信息,请参见ChangeTracker类MDN )。 The change tracker (accessible through
context.ChangeTracker
), among other things, holds a set of entities that it tracks. 变更跟踪器(可通过
context.ChangeTracker
访问)除其他外,还拥有一组跟踪的实体。 These will be the entities which are updated during SaveChanges, and technically these entities are referred to as being "attached". 这些是在SaveChanges期间更新的实体,从技术上讲,这些实体称为“附加”。
You can see the list of entities attached by type by iterating context.ChangeTracker.Entries<T>()
where T
is your type. 您可以通过迭代
context.ChangeTracker.Entries<T>()
来查看按类型附加的实体列表,其中T
是您的类型。
It depends on what kind of design pattern are you using. 这取决于您使用哪种设计模式。 If you are using Repository Pattern, the context is shared across all the repositories, and the same context tracks changes in all the entities.
如果使用的是存储库模式,则上下文在所有存储库之间共享,并且相同的上下文跟踪所有实体中的更改。 So it is OK if you call Save() 0r context.SaveChanges() once.
因此,如果您一次调用Save()0r context.SaveChanges()就可以了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.