Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8212649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:49:56+00:00 2026-06-07T10:49:56+00:00

I have a large block of code with several nested loops which needs to

  • 0

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. 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:

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:

_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. Or does the DbSet.Attach(obj) ensure that each individual instance is updated regardless on what I do with the object in my loop.

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) ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T10:49:58+00:00Added an answer on June 7, 2026 at 10:49 am

    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; 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.

    However, the best thing to do, in my opinion, is to just give it a try and see what happens!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large block of HTML that needs to be replaced, which includes
I have been writing several class templates that contain nested iterator classes, for which
We have a large C code base that has been developed over several decades.
I have some code where I frequently copy a large block of memory, often
For a research project I have a relatively large block of code that is
I have large database table, approximately 5GB, now I wan to getCurrentSnapshot of Database
I'm trying to create a faster query, right now i have large databases. My
I'm trying to work out how I can break up a large block of
I have a large (3Gb) binary file of doubles which I access (more or
I have a large segment of code, and it all works, except when there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.