question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot use dbcontext when it is in Task.Factory.StartNew();

See original GitHub issue

I’m using rc1-final-update1. The db context cannot be called in a new thread. I am importing about 100,000 records, I don’t want to wait it.

public IActionResult XXX([FromServices] SomeDbContext db)
{
    Task.Factory.StartNew(()=> {
        db.XXX.Add(new XX { xx = "123" });
        db.SaveChanges();
    });
    return Content("blablabla");
}

DbContext has been disposed in new thread.

It works with beta7, but it is no longer works after beta7.

/cc @rowanmiller @divega @natemcmaster

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
davidfowlcommented, Dec 13, 2015

That’s not a bug it’s by design. You need to make a new scope in the background thread to use the db context there. You’re introducing a race condition since the original db context might be disposed before your task runs.

2reactions
yukozhcommented, Dec 13, 2015

Thank you @davidfowl, It works with the following code.

using (var serviceScope = serviceProvider.GetRequiredService<IServiceScopeFactory>()
                        .CreateScope())
                    {
                        var context = serviceScope.ServiceProvider.GetService<ApplicationDbContext>();
                    }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Task.Factory how to create new DbContext?
Just create a new context and use await with async versions of EF rather than calling Task.Factory.StartNew() . The latter is a poor...
Read more >
Advanced Tips for Using Task.Run with Async/Await
Run with Async/Await and determine whether or not we should use Task. ... Factory.StartNew was introduced before Task.Run and is more configurable.
Read more >
Task-based asynchronous programming - .NET
In this article, learn about task-based asynchronous programming through the Task Parallel Library ... You can also use the TaskFactory.
Read more >
Cannot access a disposed object in ASP.NET Core when ...
Working with ASP.NET core I have seen this error multiple times and can be hard to debug. The error can have multiple root...
Read more >
Running Async Calls with DbContext in Entity Framework
I want to be able to run an asynchronous call as follows: [Route("doit"),ResponseType(typeof(MyModel))] public IHttpResponse PostAsyncCall(MyModel model){ ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found