EF Core - 尚未为此DbContext配置任何数据库提供程序

时间:2017-06-15 18:20:42

标签: entity-framework-6 .net-core

我是EF Core 1.0的新手。在数据库迁移期间运行以下命令时出现以下错误

命令

 migrations add ApplicationUserIsActive -c ApplicationDbContext

错误:

  

System.InvalidOperationException:没有数据库提供程序   为此DbContext配置。可以通过配置提供程序   重写DbContext.OnConfiguring方法或使用   应用程序服务提供程序上的AddDbContext。如果AddDbContext是   使用,然后还要确保您的DbContext类型接受   DbContextOptions对象在其构造函数中并将其传递给   DbContext的基础构造函数。

StartUp.cs

public class Startup
{
    public IConfigurationRoot Configuration { get; set; }

    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json");

        Configuration = builder.Build();
    }

    public void ConfigureServices(IServiceCollection services)
    {

        services.AddEntityFrameworkSqlServer()
            .AddDbContext<ApplicationDbContext>((serviceProvider, options) =>
options.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=employee;Trusted_Connection=True;MultipleActiveResultSets=true;")
       .UseInternalServiceProvider(serviceProvider));

        services.AddEntityFrameworkSqlServer()
            .AddDbContext<EmplooyeeDbContext>((serviceProvider, options) =>
options.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=employee;Trusted_Connection=True;MultipleActiveResultSets=true;")
       .UseInternalServiceProvider(serviceProvider));

        services.AddTransient<IUserContext, SeedUserContext>();
    }

}

.csproj文件

 <ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.0.0">
  <PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.DataAnnotations" Version="1.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />

       

但是当我为其他DBContext执行迁移命令时,即&#34; EmplooyeeDbContext &#34;移民称赞工作正常。

如何解决此问题?

1 个答案:

答案 0 :(得分:7)

我想出了这个问题。这是由于“ ApplicationDbContext ”的默认构造函数导致了问题。删除“ ApplicationDbContext ”的默认构造函数后,一切正常。

感谢您的支持。

相关问题