efcore: Unable to create a 'DbContext' of type ''. The exception 'Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[Project3.Data.TestContext]' while attempting to activate 'Project3.Data.TestContext'.' was thrown while attempting to create an instance. For the different patterns supported at design time
Ask a question
Remember:
I want to add database services in WPF through AddDbContext, but I found that this will result in migration failure
Include your code
https://github.com/ssccinng/TestEFWpf
Host = Microsoft.Extensions.Hosting.Host.
CreateDefaultBuilder().
UseContentRoot(AppContext.BaseDirectory).
ConfigureServices((context, services) =>
{
services.AddSingleton<MainViewModel>();
services.AddTransient<MainWindow>();
services.AddDbContext<TestContext>(option =>
{
option.UseSqlServer(context.Configuration.GetConnectionString("TestContextConnection"));
}, ServiceLifetime.Singleton, ServiceLifetime.Singleton);
})
.Build();
Include verbose output
Unable to create a 'DbContext' of type ''. The exception 'Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions1[Project3.Data.TestContext]’ while attempting to activate ‘Project3.Data.TestContext’.’ was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728`
Include provider and version information
EF Core version:8.0.0 Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer) Target framework: (e.g. .NET 8.0) Operating system: IDE: (e.g. Visual Studio 17.9.0 Preview 3.0)
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 1
- Comments: 15 (4 by maintainers)
I found that doing the migrations with the
-vflag helps, as stated here. It seems thatdotnet ef migrationswill spit out this error for any error that might occur when migrating.In my case I found out ef core migration can’t read environment variables. I constructed my connection string using values I stored in the
launchSettings.json’s environmentVariables.For anyone interested: To let ef core to read environment vars you need to manually export them like this:
export MY_VAR="my-value"Then optionally you canecho $MY_VARto check if the current terminal session has the var./cc @davidfowl More people struggling with your pattern here.
An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Some services are not able to be constructed (Error while validating the service descriptor ‘ServiceType: repository_pattern.Services.IAuthentication Lifetime: Scoped ImplementationType: repository_pattern.Services.AuthServices’: Unable to resolve service for type ‘Microsoft.AspNetCore.Identity.UserManager
1[repository_pattern.Model.ApplicationUser]' while attempting to activate 'repository_pattern.Services.AuthServices'.) Unable to create a 'DbContext' of type ''. The exception 'Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions1[repository_pattern.Data.DataContext]’ while attempting to activate ‘repository_pattern.Data.DataContext’.’ was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728 PM>experiencing this error when trying to migrate
Note for triage: this looks like a duplicate of #32835. That is,
DbContextdiscovery doesn’t work for non-ASP.NET applications that use hosting.Try removing the following from your AddDbContext call:
ServiceLifetime.Singleton, ServiceLifetime.Singleton, to let the defaults apply. You’re specifying singleton lifetime for your DbContext, which means that there will only be one DbContext instance in your entire application - that is almost surely not what you want.