简体   繁体   中英

The context cannot be used while the model is being created. EF-Core ASP.net Core2.2

I have seen many posts talking about the problem but none of them fixed my problem

the scenario DB Layer with API Controllers IDataRepository DataManagers

Code

Startup.cs

  public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddDbContext<ApplicationContext>(opts => opts.UseSqlServer(Configuration["ConnectionString:LawyerApplicationDB"]), ServiceLifetime.Transient);
        services.AddSingleton(typeof(IDataRepository<Clients, long>), typeof(ClientManager));
        services.AddSingleton(typeof(IDataRepository<Nationality, long>), typeof(NationalityManager));
        services.AddMvc();
    }

ApplicationContext

public class ApplicationContext: DbContext
{
    public ApplicationContext(DbContextOptions opts) : base(opts)
    {
    }

    public DbSet<Clients> Clients { get; set; }
    public DbSet<Nationality> Nationalities { get; set; }



}

The Manager where the error Appear

 public class NationalityManager : IDataRepository<Nationality, long>
{
    private ApplicationContext ctx; //not static

    public NationalityManager(ApplicationContext c)
    {
        ctx = c;
    }

    public Nationality Get(long id)
    {

        var nationality = ctx.Nationalities.FirstOrDefault(b => b.Id == id);
        return nationality;
    }

    public IEnumerable<Nationality> GetAll()
    {
        var nationalities = ctx.Nationalities.ToList();
        return nationalities;
    }

the error appear first time and the grid does not show data if i refresh the page the data will show

what i do wrong

this is the tutorial i used Building An ASP.NET Core Application With Web API And Code First Development

Thank you for your help

You've fallen into a classic situation where you are keeping the context around too long.

Because NationalityManager is registered as a singleton it doesn't matter that your context was registered as transient. Something with a short lifetime injected into something with a long lifetime effectively means the short lifetime is extended by the lifetime of the longer lived thing.

You can either make your manager object shorter lived or you can inject a context factory into your manager. The context factory ensures your (what should be short lived) context is created when its needed.

When you have API calls coming in at the same time, they are attempting to use the non-thread safe context simultaneously. The first call is setting up the model, and then here comes another call wanting to use the model while its being setup .

Prior to EF Core, I've addressed this issue with the original EF designed for .NET Framework. It might give you some more background.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM