Cannot access a disposed object. A common cause of this error is disposing a context

后端 未结 4 2424
梦谈多话
梦谈多话 2020-12-16 13:32

I have written a simple application and when I navigate to my edit page the below error pops up.

Microsoft.EntityFrameworkCore.Query[10100]

<
4条回答
  •  北荒
    北荒 (楼主)
    2020-12-16 14:09

    Here is what actually worked for me in a situation where I was getting the same error as above when trying to access the data in Startup.cs to run some startup data building functions on server load, rather than a specific Controller action:

            IServiceScope scope = provider.CreateScope();
            YourDbContext context = scope.ServiceProvider.GetRequiredService();
    

    In the Configure method of Startup.cs, with IServiceProvider included as a parameter:

    public async void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider provider)
    

    Just including that for anyone who wants to do the same in Startup.cs.

提交回复
热议问题