7

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>
Martin
  • 39,309
  • 62
  • 192
  • 278
  • What does your connection string look like? – DavidG Nov 22 '16 at 15:18
  • And does your context have a `DbContextOptions` object in its constructor? – DavidG Nov 22 '16 at 15:20
  • Here's the connection string. I can connect to it with SSMS. "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-MG-5880417d-c8ef-4bc8-afc5-4a7f7c617d9b;Trusted_Connection=True;MultipleActiveResultSets=true" – Martin Nov 22 '16 at 15:20
  • @DavidG - I have two ctor in ApplicationDbContext. A default one (no param) and another one with DBContextOptions: publicApplicationDbContext(DbContextOptions options) : base(options) – Martin Nov 22 '16 at 15:22
  • Do you have more than one project in your solution? Are you targeting the correct Startup Project (the one with the `Startup.cs` in it)? – haim770 Nov 22 '16 at 15:25
  • @haim770 - I do have more than one projects in my solution. However, I am running the dotnet ef command under the project folder why my ApplicationDbContext is. – Martin Nov 22 '16 at 15:35
  • Is the `project.json` file in that folder too? – DavidG Nov 22 '16 at 15:36
  • @DavidG - This is with the latest version of .NET Core. The project.json is gone. It is being replaced by a csproj file. – Martin Nov 22 '16 at 15:37
  • OK, is the connection string in the context's project or just in the consumer project? – DavidG Nov 22 '16 at 15:37

1 Answers1

22

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) {}.

Sampath
  • 63,341
  • 64
  • 307
  • 441
  • Interesting, kind of annoying though if you need to have a parameterless constructor... – DavidG Nov 22 '16 at 16:00
  • Yep,that is true.If you interest to know more about this issue please see Git : https://github.com/aspnet/EntityFramework/issues/4825 @DavidG So many options are there. – Sampath Nov 22 '16 at 16:18
  • 2
    Amazing! I ended up here while troubleshooting an issue with migrations in Asp.Net Core 1.1 and using a [workaround](https://github.com/aspnet/Tooling/blob/master/known-issues-vs2017.md#publishing-project-with-entity-framework-migration-fails) to generate the migration code. I had been getting the "No database provider has been configured..." error for a while and couldn't make heads or tails of it. Finally, I landed here and saw to **remove the default constructor** from my context. That was it! Thanks @Sampath! – ih303 Mar 30 '17 at 17:55
  • 1
    Doesn´t this contradict this: http://stackoverflow.com/questions/38338475/no-database-provider-has-been-configured-for-this-dbcontext-on-signinmanager-p – Marcus Apr 23 '17 at 01:31