I'm developing a Web Application for process a large amount of data and create a .csv file with those items.
So far, so good, but, it occored to me that it's not really fast this proccess, so I decide to make all those things in Task (Threading).
Like This:
_ = Task.Run(() => {
try
{
LetsGoCrazy(model);
}
catch (Exception ex)
{
SaveLog(model, "Error dumbass", ex);
}
});
My question is that, how can I update my database (Entity Framework Core) inside a Task Method?
Because, it shows me this Exception:
System.ObjectDisposedException: 'Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.
I believe that it must have another way to accomplish that without recreating the Entity Framework Context inside the Task.