我是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 file
<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" 时,迁移命令可以正常工作。
我该如何解决这个问题?
Microsoft.EntityFrameworkCore.SqlServer
的包引用吗? - dbattagliaApplicationDbContext
,然后再添加两个AddDbContext
吗?看起来你正在相互覆盖。 - VMAtm