没有为此DbContext配置数据库提供程序 [英] No database provider has been configured for this DbContext

查看:527
本文介绍了没有为此DbContext配置数据库提供程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近将项目更新为最新版本的Entity Framework Core(+ VS2017)。当我尝试更新数据库时,出现以下错误消息。该错误消息是明确的,但它似乎是错误的。我的ConfigureServices中确实有一个AddDbContext(请参见下面的代码)。

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

我缺少什么?

错误

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


推荐答案

您必须删除默认构造函数。换句话说,参数较少的构造函数。之后所有这些都将按预期工作。

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

注意:在运行时调用参数较少的构造方法,而不是此 public MyDbContext(DbContextOptions options):base(options){}

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

这篇关于没有为此DbContext配置数据库提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆