运行 Entity Framework 中使用 DbContext 的异步调用
Running Async Calls with DbContext in Entity Framework
我希望能够 运行 异步调用如下:
[Route("doit"),ResponseType(typeof(MyModel))]
public IHttpResponse PostAsyncCall(MyModel model){
//Code removed for simplicity
Task.Factory.StartNew(() => asyncStuff(model.id);
return OK(model);
}
private void asyncStuff(int id) {
MyModel model = db.MyModels.find(id);
//Do things here. Long call to other webservices/processing of data. Time intensive normally.
User user = db.Users.find(model.userId);
//Do more things. Time intensive normally.
}
但是,当异步方法命中 db.
方法时,会发生错误:
An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code
Additional information: The operation cannot be completed because the DbContext has been disposed.
使用的上下文在这里:
public class MyContext : DbContext, MyContextInterface
{
// You can add custom code to this file. Changes will not be overwritten.
//
// If you want Entity Framework to drop and regenerate your database
// automatically whenever you change your model schema, please use data migrations.
// For more information refer to the documentation:
// http://msdn.microsoft.com/en-us/data/jj591621.aspx
public MyContext() : base("name=MyContext")
{
}
public System.Data.Entity.DbSet<MyAPI.Models.User> Users { get; set; }
public System.Data.Entity.DbSet<MyAPI.Models.Request> Requests { get; set; }
public void MarkAsModified(Object item)
{
Entry(item).State = EntityState.Modified;
}
}
上下文在控制器中创建为 class 变量,通过:
private MyContextInterface db = new MyContext();
我可以看到在方法结束时正在处理数据库上下文;但是,我需要在异步方法运行期间保留此上下文以访问所需的信息。我该怎么做?
你开始了一个任务,但你没有等待它,所以你立即调用 OK。
当 long 运行 任务完成时,您的数据库上下文确实已被释放。
加个await,问题就迎刃而解了。
我希望能够 运行 异步调用如下:
[Route("doit"),ResponseType(typeof(MyModel))]
public IHttpResponse PostAsyncCall(MyModel model){
//Code removed for simplicity
Task.Factory.StartNew(() => asyncStuff(model.id);
return OK(model);
}
private void asyncStuff(int id) {
MyModel model = db.MyModels.find(id);
//Do things here. Long call to other webservices/processing of data. Time intensive normally.
User user = db.Users.find(model.userId);
//Do more things. Time intensive normally.
}
但是,当异步方法命中 db.
方法时,会发生错误:
An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code
Additional information: The operation cannot be completed because the DbContext has been disposed.
使用的上下文在这里:
public class MyContext : DbContext, MyContextInterface
{
// You can add custom code to this file. Changes will not be overwritten.
//
// If you want Entity Framework to drop and regenerate your database
// automatically whenever you change your model schema, please use data migrations.
// For more information refer to the documentation:
// http://msdn.microsoft.com/en-us/data/jj591621.aspx
public MyContext() : base("name=MyContext")
{
}
public System.Data.Entity.DbSet<MyAPI.Models.User> Users { get; set; }
public System.Data.Entity.DbSet<MyAPI.Models.Request> Requests { get; set; }
public void MarkAsModified(Object item)
{
Entry(item).State = EntityState.Modified;
}
}
上下文在控制器中创建为 class 变量,通过:
private MyContextInterface db = new MyContext();
我可以看到在方法结束时正在处理数据库上下文;但是,我需要在异步方法运行期间保留此上下文以访问所需的信息。我该怎么做?
你开始了一个任务,但你没有等待它,所以你立即调用 OK。 当 long 运行 任务完成时,您的数据库上下文确实已被释放。 加个await,问题就迎刃而解了。