Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entries being re-attached after disposing of dbcontext #17901

Closed
arturgromek opened this issue Sep 18, 2019 · 2 comments
Closed

Entries being re-attached after disposing of dbcontext #17901

arturgromek opened this issue Sep 18, 2019 · 2 comments

Comments

@arturgromek
Copy link

I am bulk inserting data into a sql server db. I add a few hundred records at a time, call SaveChanges, and dispose of the context. Then create a new Context to add the next few hundred.

After disposing and creating the new context, the change tracker tracks 0 entries, as expected. Upon adding the next first object to the new context with Update, the context picks up the previously added entries, and tracks them with a state of unchanged. Since I'm adding tens of thousands of records, after adding a few thousand, the dbcontext slows down significantly, as it is tracking lots of unchanged records.

I have AutoDetectChangesEnabled set to false, and am using auto-generated Ids.

Steps to reproduce

  • Create DbContext
  • add items with Update
  • savechanges
  • dispose of context
  • note that change tracker is not tracking anything
  • add a new item with Update
  • check change tracker, see that previously added entries are there.

How I'm saving with/disposing of the context.

try
{
    var result = await DbContext.SaveChangesAsync();
    return result;
}
catch (Exception e)
{
    throw e;
}
finally
{
    DbContext.Dispose();
    DbContext = new MyContext();
    DbContext.ChangeTracker.AutoDetectChangesEnabled = false;
}

Notes

I've seen this comment here: #6737 (comment) that mentions the garbage collector needing to run to release entries from memory. But is this relevant with a new context tracking old entries?

To get around this issue I've been manually detaching the entries before disposing. Which does solve it, but isn't ideal as acknowledged in this thread: #10125 (comment)

I realise that my usage isn't common and the issue isn't really blocking but bringing it up in case it's a symptom of a bigger bug somewhere.

Further technical details

EF Core version: 3.0.0-preview9.19423.6
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.0
Operating system: Windows 10 64 bit
IDE: Visual Studio 2019 16.3 preview 3

@AndriySvyryd
Copy link
Member

@arturgromek Could you create a small runnable project that shows this issue?

@smitpatel
Copy link
Contributor

EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.

BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants