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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:22:57+00:00 2026-06-04T07:22:57+00:00

I am using Entity Framework through a Repositories pattern and have a significant and

  • 0

I am using Entity Framework through a Repositories pattern and have a significant and surprising performance problem. I have done profiling so I have a pretty good idea of what happens, I just don’t know what to do about it.

Here is the essense of my code (simplified):

var employee = Repositories.Employees.FirstOrDefault(s => s.EmployeeId == employeeId);
employee.CompanyId = null;
Repositories.Commit();

The middle line (employee.CompanyId = null) takes an astounding amount of time to complete (around 30 seconds). The time is NOT spent on the Commit line.

Through profiling, I have found the reason to be running this part of the auto generated EF code:

if (previousValue != null && previousValue.**Employees**.Contains(this))
{
    previousValue.Employees.Remove(this);
}

That doesn’t really help me, but it does confirm that the problem lies in the EF. I would really like to know what to do. I can update the column in other ways (stored procedure) but I would really rather use the EF everywhere.

I cannot easily edit the EF settings, so I would prefer suggestions that does not involve this.

Update
I solved the problem by running SQL directly against the database and then refreshing the object from context to make sure EF would detect this change immediately.

public void SetCompanyNull(Guid employeeId)
{
    _ctx.ExecuteStoreCommand("UPDATE Employee SET CompanyId = NULL WHERE EmployeeId = N'" + employeeId + "'");
    _ctx.Refresh(RefreshMode.StoreWins, _ctx.Employees.FirstOrDefault(s => s.EmployeeId == employeeId));
}

Update 2
I ALSO solved the problem by disabling lazy loading temporarily.

var lazyLoadDisabled = false;
if (_ctx.ContextOptions.LazyLoadingEnabled)
{
   _ctx.ContextOptions.LazyLoadingEnabled = false;
   lazyLoadDisabled = true;
}

this.GetEmployeeById(employeeId).CompanyId = null;
this.SaveChanges();

if (lazyLoadDisabled)
{
    _ctx.ContextOptions.LazyLoadingEnabled = true;
}

I am really curious about WHY it’s so much faster with lazy loading disabled (and which side-effects this might have)

  • 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-04T07:22:58+00:00Added an answer on June 4, 2026 at 7:22 am

    This is problem in EF POCO Generator Template which causes unexpected lazy loading in some scenarios. This template generates fixup code for navigation properties so if you change the navigation property on one side it internally goes to other end of the changed relation and tries to fix the relation to be still consistent. Unfortunately if your related object doesn’t have navigation property loaded it triggers lazy loading.

    What you can do:

    • Modify template and remove all code related to fixup
    • Remove reverse navigation property (Employees) from your Company
    • Turn off lazy loading prior to this operation – context.ContextOptions.LazyLoadingEnabled = false
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Entity Framework in my project, and I have the problem that, once
I keep getting an InvalidProgramException trying to grab data through the entity framework. using
I'm using Entity Framework Code First v 4.3.0 I have a two entities with
I have come across what appears to be very peculiar behaviour using entity framework
I have an app that is saving to a db (using Entity Framework) and
I'm using Entity Framework 4 and I have created a UnitOfWork class that creates
I have to delete some rows in the database. I'm using Entity Framework 4.3
I am using the Entity Framework and SQL Server Compact Edition through the code
Using the Entity Framework that I generated. I have a Roles table created during
I'm using Entity Framework 4 and WPF with the MVVM pattern. Now I'm facing

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.