Several Request with EF Core in Net Core

0

I have the following problem.

I have a WebApi Rest in .Net Core where I use EntityFramework Core, I make a request and it works very well, the mess is when I make two requests at the same time, apparently it only supports one at a time, if I do two it generates me the next error attached.

Se ha producido una excepción no controlada por el usuario: CLR/System.InvalidOperationException    
An exception of type 'System.InvalidOperationException' occurred in LaboratorioWebApi.dll but was not handled in user code:     
'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.'

I really do not understand that error nor do I know what to change, I just start with .Net Core and EntityFramework Core, I'm using dependency injection.

I appreciate that you can guide me to know what I should do in those cases.

In my Startup this is the method where I do the injection.

public void ConfigureServices(IServiceCollection services)
{
           services.AddMvc();

            var laboratorio = Configuration.GetValue<string>("DbInfo:Laboratorio");
            var soft = Configuration.GetValue<string>("DbInfo:Soft");

            services.AddDbContext<LaboratorioContext>(options => options.UseSqlServer(laboratorio));
            services.AddDbContext<SofContext>(options => options.UseSqlServer(soft));

            // Inyeccion de Dependencias            
            services.AddSingleton<EquipoRepository>();
            services.AddSingleton<TipoEquipoRepository>();
            services.AddSingleton<MarcaRepository>();
            services.AddSingleton<EmpleadoRepository>();
            services.AddSingleton<RangoRepository>();
        }

and this is the context

using Microsoft.EntityFrameworkCore;

namespace LaboratorioWebApi.Models
{
    public class LaboratorioContext : DbContext
    {
        public LaboratorioContext(DbContextOptions<LaboratorioContext> options)
        : base(options) { }

        public DbSet<Equipo> Equipo { get; set; }
        public DbSet<EquipoDetalle> EquipoDetalle { get; set; }
        public DbSet<Laboratorio> Laboratorio { get; set; }
        public DbSet<Marca> Marca { get; set; }
        public DbSet<Rango> Rango { get; set; }
        public DbSet<Responsable> Responsable { get; set; }
        public DbSet<TipoEquipo> TipoEquipo { get; set; }
    }

    public class SoftContext : DbContext
    {
        public SoftContext(DbContextOptions<SoftContext> options)
        : base(options) { }
        public DbSet<Empleado> Empleado { get; set; }
    }
}

I hope you can help me. Thank you in advance

    
asked by Cristian 12.05.2017 в 01:34
source

1 answer

1

The error occurs because you are accessing the same instance of DbContext from several threads at the same time. Doing that is not correct, because DbContext is not designed for your instances to be used from more than one thread. Each thread needs to use a separate instance of DbContext .

Although it is not 100% clear with the code you included, but I think it is reasonable to assume that your repositories are designed to receive DbContext in their respective constructs using dependency injection. This in itself is not a problem. The problem is that you are configuring your repositories as singletons.

So if 2 threads need to make a query through one of your repositories, both threads will receive the same instance of the repository, which by extension means that both threads are going to user the same instance of DbContext . This is not correct and it will cause you the error you receive.

There may be a better way to fix this, but instead, I would try changing the configuration of the repositories to be transient instead of singleton :

services.AddTransient<EquipoRepository>();
services.AddTransient<TipoEquipoRepository>();
services.AddTransient<MarcaRepository>();
services.AddTransient<EmpleadoRepository>();
services.AddTransient<RangoRepository>();

And of course, if you have configured other classes such as singleton that in turn have a dependency, direct or indirect, to the repositories, these would also change them to be transient .

    
answered by 12.05.2017 / 02:36
source