沒有為此DbContext配置數據庫提供程序

entity-framework entity-framework-core

我最近將我的項目更新為最新版本的Entity Framework Core(+ VS2017)。當我嘗試更新數據庫時,我收到以下錯誤消息。錯誤消息很明顯,但似乎是錯誤的。我的ConfigureServices中有一個AddDbContext(參見下面的代碼)。

我錯過了什麼?

錯誤

> 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.

啟動

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>

一般承認的答案

你必須刪除默認的構造函數 。換句話說,參數less constructor.After所有將按預期工作。

注意:原因是,在運行時調用參數less構造函數而不是此public MyDbContext(DbContextOptions options) : base(options) {}



Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow