Active2 years, 3 months ago
There're two projects in my solution as below:
- Sample.UI: a dotnet core console project created by 'dotnet new'.
- Sample.Infrastruction: a dotnet core library created by 'dotnet new -t lib', the BlogDbContext is right there.
When I try to run the
dotnet ef --startup-project ..sample.ui migrations add InitialDB
in the Sample.Infrastructure folder, it raised an error:From a command prompt or shell, run dotnet restore followed by dotnet run to run the sample. About the code. The code included in this sample is intended to get you quickly started with a.NET Core console application that connects to Azure DocumentDB. More information. Azure DocumentDB Documentation; Azure DocumentDB.NET SDK.
No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.On Configuring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext
The error stack information:
at Microsoft.EntityFrameworkCore.Internal.DatabaseProviderSelector.SelectServices() at Microsoft.EntityFrameworkCore.Internal.LazyRef1.get_Value()
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Vis itScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass16_0.b__0(ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
at Microsoft.EntityFrameworkCore.Design.Internal.DesignTimeServicesBuilder.Build(DbContext context)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
I have tried to add AddDbContext to fix the issue, but didn't work for me. Maybe I'm not using the built-in DI correctly. I have googled a lot, but most solutions are used for asp.net core rather then dotnet console.
For your reference, below is the Sample.Infrastructure source code:
Sample.UI source code:
Update
I have also tried @Morten's solution, execute the following command in Sample.UI folder. However, I got the exactly same error.
Charlie
CharlieCharlie
2 Answers
To avoid most commons mistakes when doing EF Core migration, do the following
- explicitly write the project parameters in command
--startup-project < StartProject >, Sample.UI in your case
--project < ContextProject >, this is the project where your DBContext leaves, Sample.Infrastructure in your case - migrations work with
public default constructor
orIDbContextFactory
if default is absent.So if using default constructor you'll have to overrideOnConfiguring
in your context class, something like
alternativly you can implement
IDbContextFactory
in the same project where your BlogDbContext resides nsbnsb
You need to add
Sample.Infrascruture
as --project
. Like this:And then run it from the
Sample.UI
projectThe same when you do a
database upgrade
Also, try to set the connectionstring in
Morten OCMorten OCBlogDbContext
1,73911 gold badge2020 silver badges2424 bronze badges