没有为此DbContext配置数据库提供程序

Mar*_*tin 6 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.
Run Code Online (Sandbox Code Playgroud)

启动

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

的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>
Run Code Online (Sandbox Code Playgroud)

Sam*_*ath 17

你必须删除默认的构造函数.换句话说,参数less constructor.After所有将按预期工作.

注意:原因是,在运行时调用参数less constructor而不是this public MyDbContext(DbContextOptions options) : base(options) {}.

  • 惊人!我在解决Asp.Net Core 1.1中的迁移问题并使用[解决方法](https://github.com/aspnet/Tooling/blob/master/known-issues-vs2017.md#publishing-project)时结束了这里-with-entity-framework-migration-failed)生成迁移代码.我有一段时间没有得到"没有数据库提供程序已经配置..."错误,并且无法做出正面或反面.最后,我登陆这里,看到**从我的上下文中删除默认构造函数**.就是这样!谢谢@Sampath! (2认同)