简体   繁体   English

在这种情况下,我应该期待什么样的持久性行为

[英]What persistence behavior should I expect when in this scenario

I have a large block of code with several nested loops which needs to ultimately update many records in the database. 我有一大堆带有几个嵌套循环的代码,它们最终需要更新数据库中的许多记录。 I am trying to minimize the number of SaveChanges() calls to Entity Framework. 我正在尝试减少对Entity Framework的SaveChanges()调用的次数。 Also note that we we are using the repository pattern. 另请注意,我们正在使用存储库模式。

Essentially I'm iterating a collection and need to update both the items in the collection and upon each iteration, another object retrieved from the db and contextualized by the item from the collection. 本质上,我正在迭代一个集合,并且需要更新集合中的项目,并且在每次迭代时,需要从数据库中检索另一个对象,并通过集合中的项目对其进行上下文化。

Sample code: 样例代码:

foreach (var outer in outerList)
            {
                obj = unit.GetRepository<MyObj>().Get(s =>
                    s.id = myId
                    ).SingleOrDefault();

                obj.value += outer.value;                    
                outer.objId = obj.objId;

                unit.GetRepository<MyOuterObj>().Update(outerObj);
                unit.GetRepository<MyObj>().Update(obj);                    
            }
            unit.Save();

The call to Update() performs the following: 对Update()的调用执行以下操作:

public virtual void Update(T entityToUpdate)
    {
        if(entityToUpdate is AuditModelBase)
        {
            var model = entityToUpdate as AuditModelBase;
            model.UpdatedAt = DateTime.UtcNow;
        }

        DbSet.Attach(entityToUpdate);
        Context.Entry(entityToUpdate).State = EntityState.Modified;
    }

And the call to Save() of course performs the following: 对Save()的调用当然会执行以下操作:

_context.SaveChanges();

So my question is, as I'm reassigning obj to a different value each time through the loop, do I need Save() inside the foreach loop in order for all instances of "obj" to persist. 所以我的问题是,由于每次通过循环将obj重新分配给一个不同的值时,是否需要foreach循环内的Save()才能使“ obj”的所有实例持久存在。 Or does the DbSet.Attach(obj) ensure that each individual instance is updated regardless on what I do with the object in my loop. 还是DbSet.Attach(obj)确保每个单独的实例都得到更新,而不管我对循环中的对象执行什么操作。

Or perhaps a better way to ask this is: 也许更好的方法是:

Given that it looks like Attach() is pass-by-reference so therefore only my last obj will be updated, what are best practices with EF to accomplish this sort of thing (excluding the option of straight calls to SQL) ? 鉴于Attach()看起来像是传递引用,因此仅更新了我的最后一个obj,使用EF来完成此类操作的最佳实践是什么(不包括直接调用SQL的选项)?

I don't think you have to worry. 我认为您不必担心。 Once you call Attach on your object, it doesn't matter whether you keep a reference to it yourself or not; 一旦在对象上调用“ Attach ”,无论您自己保留引用还是无关紧要; it should be fine. 应该没问题

The thing to keep in mind is that your object lives as long that it's referenced by someone . 要记住的是,您的对象存在的时间可以被某人引用。 So, it stands to reason that calling Attach would cause your DbSet to reference your object, thus keeping it alive even after you don't reference it anymore yourself. 因此,可以肯定的是,调用Attach将使您的DbSet引用您的对象,从而即使您不再自己引用该对象也可以使该对象保持活动状态。

However, the best thing to do, in my opinion, is to just give it a try and see what happens! 但是,我认为最好的办法就是尝试一下,看看会发生什么!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM