简体   繁体   English

我需要多久保存一次上下文?

[英]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: vendorOrderRepositoryorderRepositoryrepositorty都有自己的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的基本理解,但是我跳过了这一部分...)

Edit 1 编辑1

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. 因此,如果vendorOrderRepositoryorderRepository共享相同的上下文,则对其进行一次调用将保存所做的所有更改。

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.

相关问题 我需要 context.Request.EnableBuffering 吗? - Do I need context.Request.EnableBuffering? 如何在工具部件(在Webpart中)中封装经常重复的代码? - How can I do encapsulated often repetitive code in a toolpart (in webpart)? 我需要保存什么属性到RoamingSettings? - What attribute do I need to save to RoamingSettings? 如何为数据容器实现“我是否需要保存”机制? - how do I implement a “Do I Need To Save” mechanism for a data container? 如何使用EF DB上下文在ViewModel中保存对象列表? - How do i save a list of objects in my ViewModel using EF DB Context? 如果需要2个提交按钮,如何保存MVC4模型? - How do I save my MVC4 model if I need 2 submit buttons? 我是否需要 OnModelCreating 中的实体数据 model 用于运行时数据库上下文? - Do I need entity data model in OnModelCreating for runtime DB context? 为什么我需要ToList()来避免处置上下文错误? - Why do I need a ToList() to avoid disposed context errors? 如何计算在 10 毫秒内调用方法的频率? - How do I count how often a method was called within 10 milliseconds? 如何根据字符在字符串中出现的频率对字符列表进行排序? - How do I sort a list of chars based on how often they appear in a string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM