Scoped services require scope to be resolved. You can create scope via ServiceProviderServiceExtensions.CreateScope
:
using(var scope = app.Services.CreateScope())
{
var dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
// use context
}
More Related Contents:
- Get SQL code from an Entity Framework Core IQueryable
- Many-to-many self referencing relationship
- How to mock an async repository with Entity Framework Core
- ASP.NET Core 6 how to access Configuration during startup
- navigation property should be virtual – not required in ef core?
- Cannot access a disposed object in ASP.NET Core when injecting DbContext
- How to instantiate a DbContext in EF Core
- Filter all queries (trying to achieve soft delete)
- The entity type ‘Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin’ requires a key to be defined
- How to add the same column to all entities in EF Core?
- Unable to create migrations after upgrading to ASP.NET Core 2.0
- Dynamically changing schema in Entity Framework Core
- Cascade deleting with EF Core
- EF CORE 2.1 HasConversion on all properties of type datetime
- Include with FromSqlRaw and stored procedure in EF Core 3.1
- Integration test and hosting ASP.NET Core 6.0 without Startup class
- The LINQ expression could not be translated and will be evaluated locally
- Entity Framework Core: Guid Greater Than for Paging
- Unable to edit db entries using EFCore, EntityState.Modified: “Database operation expected to affect 1 row(s) but actually affected 0 row(s).”
- Add migration with different assembly
- EF models. Navigation properties can only participate in a single relationship
- ef core doesn’t use ASPNETCORE_ENVIRONMENT during update-database
- How to load navigation properties on an IdentityUser with UserManager
- How to get user information in DbContext using Net Core
- How to create a password protected database?
- Implementing recursive property loading in EF Core
- How to set Identity Seed value in code-first?
- EF Core takes a lot of time, sometimes, to perform SELECT query
- How to resolve .net core build error NETDSDK1061 and warning MSB3277
- Publish error: Found multiple publish output files with the same relative path