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 7916709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:54:14+00:00 2026-06-03T14:54:14+00:00

This is my first EF project so bear with me please. When updating an

  • 0

This is my first EF project so bear with me please.

When updating an entity such as Department, you pull it from the context, update its values and call context.SaveChanges. However, if you update Department.Employees, EF does not find that funny.

I searched and came up with the option of setting Multipleactiveresultsets=true in the connection string but want to know if:

  • Is this the recommended way?
  • Does this adversely affect performance / what should I look out for?
  • 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-03T14:54:17+00:00Added an answer on June 3, 2026 at 2:54 pm

    Enabling MARS is only necessary if you want to execute multiple queries on the same connection in parallel. This happens if you do something like this:

    /* Foreach uses an iterator over the resultset of your query, but the query is not fetched
       immediately, instead the iterator internally triggers fetching for single
       processed record from opened data reader. Because of that the query and the reader
       are active until the iteration is over. */
    foreach (var department in context.Departments.Where(...))
    {
        /* The first query is still active on the connection but now you are executing
           lazy loading of all related employees =>. You are executing a second query and,
           without MARS, you will get an exception. */
        var employee = department.Employees.FirstOrDefault(...);
    }
    

    How to avoid that?

    • Use eager loading instead of lazy loading: context.Departments.Include(d => d.Employees)
    • Materialize whole department’s result set prior to using lazy loading. It means not accessing employees inside of the loop.
    • Enable MARS and the mentioned example will simply work

    Is this the recommended way? Does this adversely affect performance /
    what should I look out for?

    It depends on the problem you are trying to solve. If you have multiple departments to process, accessing their employees collection will trigger a separate query for each department. That is called N+1 problem – you have N departments and one query to fetch them and for each department you will execute one additional query => N+1 queries. For a huge number of departments this will be a performance killer.

    Eager loading is not a bullet proof solution either. It can affect performance as well. Sometimes you simply need to execute separate queries to fetch all necessary departments and separate queries to fetch all necessary employees. If you have lazy loading turned off it should fix your relations and fill Employees property correctly for you. Btw, I made a suggestion on Data UserVoice to support this feature out of the box.

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

Sidebar

Related Questions

This is my first time deploying a project so please bear with me. I
Imagine this as the code from build.xml: <project name=test project> <target name=first> <echo>first</echo> </target>
This is my first time trying to use any SCM so please bear with
This is the first time I've used NHibernate for a big project so bear
this is my first time trying to use github so please bear with me..
This is my first azure project and I'm not sure if I'm doing something
This is my first applet project and for some reason when I try to
This was my first Sinatra project - link shortener but I am stuck with
This is my first time doing this sort of project so apologies if the
This is the first time I'm creating an open-source project, and I've decided (based

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.