entity-framework-6 - EF Core - 尚未为此 DbContext 配置数据库提供程序

标签 entity-framework-6 .net-core

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

命令

 migrations add ApplicationUserIsActive -c 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 object in its constructor and passes it to the base constructor for 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 即“EmplooyeeDbContext”执行迁移命令时,迁移命令工作正常。

如何解决这个问题?

最佳答案

我发现了这个问题。这是由于“ApplicationDbContext”的默认构造函数导致了该问题。删除“ApplicationDbContext”的默认构造函数后,一切都开始正常工作。

感谢大家的支持。

关于entity-framework-6 - EF Core - 尚未为此 DbContext 配置数据库提供程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44574358/

相关文章:

entity-framework - EF6 alpha 异步等待实体存储过程/函数导入?

c# - 使用 EF6 为 CRUD 操作实现通用服务

c# - Entity Framework 按时间过滤数据

c# - Entity Framework 使用 IdentityDbContext 和代码优先自动迁移表位置和模式?

c# - EntityState.Modified 在 EF Core 和 EF 6 中更新时的工作方式不同

iis - 将 .Net Core 发布到 IIS - 进程无法访问文件

visual-studio-2017 - VS2017 nuget加载项目依赖失败

azure - 如何避免 .Net Core 上的 Azure 表存储/Cosmos DB 表 API 的启动延迟

c# - .NET core 3.0 System.IO.Ports SerialPort 在 RPI 上始终使用 5-10% CPU

c# - 命令行解析器 NUGet 包使简单的示例程序工作