Entity framework DBcontext准备好使用了吗?
在我用asp.net core 2.1开发的应用程序中,我使用了Entity framework DBcontext准备好使用了吗?,entity-framework,asp.net-core,thread-safety,Entity Framework,Asp.net Core,Thread Safety,在我用asp.net core 2.1开发的应用程序中,我使用了实体框架,从而使用了ApplicationDB上下文。 我经常会遇到这样的错误: a second operation started on this context before a previous operation completed. 有没有办法确定上下文是否被其他方法使用?差不多 if(_contex.isAvailable()) 我不使用异步方法 编辑: 我使用DependecyInjection- private
实体框架
,从而使用了ApplicationDB上下文
。
我经常会遇到这样的错误:
a second operation started on this context before a previous operation completed.
有没有办法确定上下文是否被其他方法使用?差不多
if(_contex.isAvailable())
我不使用异步方法
编辑:
我使用DependecyInjection-
private ApplicationDbContext _context { get; set; }
public BooksRepository(IConfiguration configuration, ApplicationDbContext context)
{
Configuration = configuration;
_context = context;
}
还有我的Startup.cs
var dbContext = LoadedServices.GetService<ApplicationDbContext>();
services.AddSingleton<BooksRepository>(new BooksRepository(LoadedServices.GetService<IConfiguration>(), dbContext));
var dbContext=LoadedServices.GetService();
AddSingleton(新的BooksRepository(LoadedServices.GetService(),dbContext));
我不使用多线程(我不知道)我认为您可能正在打开一个上下文,而不是处理该上下文,或者在开始另一个线程之前保存更改。有几件事:您是如何创建上下文的,您的上下文是否从多个线程访问?请注意DbContext不是线程安全的:我已经编辑了我的问题