使用依赖项注入时,如何解决“在此操作之前在此上下文上启动第二项操作...”? [英] How to fix 'A second operation started on this context before a previous operation completed...' when working with dependency injection?

查看:818
本文介绍了使用依赖项注入时,如何解决“在此操作之前在此上下文上启动第二项操作...”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当从数据库中读取数据时,出现以下错误:

when reading data from the database I get this error:


在此上下文中,第二个操作在上一个操作$ b $之前开始b完成。不保证任何实例成员都是线程安全的。

A second operation started on this context before a previous operation completed. Any instance members are not guaranteed to be thread safe.

我有以下ApplicationContext.cs:

I have the following ApplicationContext.cs:

public class ApplicationContext : Microsoft.EntityFrameworkCore.DbContext
{
    public ApplicationContext(DbContextOptions<ApplicationContext> options)
        : base(options)
    { }

    public DbSet<MyClass> MyClasses{ get; set; }
}   

以下ApplicationContextFactory.cs

The following ApplicationContextFactory.cs

public class ApplicationContextFactory : IDesignTimeDbContextFactory<ApplicationContext>
{
    public ApplicationContext CreateDbContext(string[] args)
    {
        var builder = new DbContextOptionsBuilder<ApplicationContext>();
        var connection = "myConnectionString";

        builder.UseSqlServer(connection);

        return new ApplicationContext(builder.Options);
    }
}   

以下ServiceLoader.cs(在此声明DI ):

The following ServiceLoader.cs (where I declare the DI):

public static class ServiceLoader
{
    public static void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton<IRepository, Repository>();

        var connection = "myConnectionString";
        services.AddDbContext<ApplicationContext>(options => options.UseSqlServer(connection));
    }
}

最后是以下存储库,其中例外是抛出:

and finally, the following Repository, where the exception is thrown:

public class Repository : IRepository
{
    private ApplicationContext _db;

    public Repository (ApplicationContext db)
    {
        _db = db;
    }

    public List<MyClass> Get()
    {
        _db.MyClasses.ToList();
    }
}

我也试图将存储库声明为瞬态Singleton,但抛出了类似的错误

I have also tried to declare the Repository as Transient instead of Singleton, but a similar error is thrown


'尝试在配置上下文时使用上下文。无法在OnConfiguring内部使用DbContext实例,因为此时仍在对其进行配置。如果在上一个操作完成之前在此上下文上启动了第二个操作,则会发生这种情况。

'An attempt was made to use the context while it is being configured. A DbContext instance cannot be used inside OnConfiguring since it is still being configured at this point. This can happen if a second operation is started on this context before a previous operation completed. Any instance members are not guaranteed to be thread safe.'

关于如何解决此问题的任何想法?谢谢!

Any idea on how to fix this? Thanks!

推荐答案

对于我来说,我发现以下信息很有帮助:

In my case I found the following information helpful:

https://docs.microsoft.com/zh-我们/ ef / core / miscellaneous / configuring-dbcontext

并在启动时使用重载的AddDbContext方法将Db Context的生存期范围更改为瞬态: / p>

And changed the lifetime scope of my Db Context to transient using the overloaded AddDbContext method in startup:

services.AddDbContext<MyAppDbContext>(options => {
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection"));
        }, ServiceLifetime.Transient);

这篇关于使用依赖项注入时,如何解决“在此操作之前在此上下文上启动第二项操作...”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆