Ich habe kürzlich mein Projekt auf die neueste Version von Entity Framework Core (+ VS2017) aktualisiert. Wenn ich versuche, die DB zu aktualisieren, erhalte ich die folgende Fehlermeldung. Die Fehlermeldung ist klar, aber sie scheint falsch zu sein. Ich habe einen AddDbContext in meinem ConfigureServices (siehe Code unten).
Was vermisse ich?
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.
Anfang
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>
Sie müssen den Standardkonstruktor entfernen . Mit anderen Worten Parameter weniger Konstruktor. Danach wird alles wie erwartet funktionieren.
Hinweis: Der Grund dafür ist, dass zur Laufzeit anstelle des public MyDbContext(DbContextOptions options) : base(options) {}
der Parameter less-Konstruktor aufgerufen wird public MyDbContext(DbContextOptions options) : base(options) {}
.