Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No database provider has been configured for this DbContext

I recently updated my project to the latest version of Entity Framework Core (+VS2017). When I try to update the DB I get the following error message. The error message is clear, but it appears to be wrong. I do have a AddDbContext in my ConfigureServices (see code below).

What am I missing?

Error

> dotnet ef database update --verbose

Finding DbContext classes...
Using context 'ApplicationDbContext'.

System.InvalidOperationException: No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.

Startup

public void ConfigureServices(IServiceCollection services) {
  services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(this.Configuration.GetConnectionString("DefaultConnection")));

CSProj

<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore">
  <Version>1.1.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore">
  <Version>1.1.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer">
  <Version>1.1.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design">
  <Version>1.1.0</Version>
  <PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools">
  <Version>1.0.0-msbuild1-final</Version>
  <PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design">
  <Version>1.1.0</Version>
</PackageReference>
like image 400
Martin Avatar asked Nov 22 '16 15:11

Martin


People also ask

What is IDesignTimeDbContextFactory?

IDesignTimeDbContextFactory<TContext> InterfaceImplement this interface to enable design-time services for context types that do not have a public default constructor. At design-time, derived DbContext instances can be created in order to enable specific design-time experiences such as Migrations.

What is services AddDbContext?

AddDbContext<TContext>(IServiceCollection, ServiceLifetime) Registers the given context as a service in the IServiceCollection. You use this method when using dependency injection in your application, such as with ASP.NET.

How do you use UseInMemoryDatabase?

Inject ApiContext to the CategoryController via constructor. Create a GET method which will call GetCategories of ApiContext and return the data. Configure ApiContext in the startup class with the option UseInMemoryDatabase. Now, run the application and you will be able to see all the added categories.


1 Answers

You have to remove the default constructor.In other words parameter less constructor.After that all will work as expected.

Note : The reason for that is, the parameter less constructor is being called at run time instead of this public MyDbContext(DbContextOptions options) : base(options) {}.

like image 179
Sampath Avatar answered Sep 19 '22 17:09

Sampath