在Asp.Net Core中使用两个上下文进行数据库迁移(Docker上的SQL Server)

我的应用程序中有两个上下文。 一个在WebApi Project -> ApiContext (inherited from IdentityDbContext) ,另一个在Infrastructure -> Context

我使用Docker Engine( microsoft/mssql-server-linux )的Linux上的Microsoft SQL Server的官方图像。

我写了一个DbFactory 。 下面你可以find我的代码。

ApiContext:内部类ApiContext:IdentityDbContext {公共ApiContext(DbContextOptions选项):基地(选项){Database.EnsureDeleted(); }

  protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); } } 

上下文

内部类上下文:DbContext {公共上下文(DbContextOptions选项):基地(选项){Database.EnsureCreated(); }

 public DbSet<Reservation> Reservations { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<Table>().ToTable("Table"); } 

}

在我的基础设施项目中,我曾经说过DbFactory

 public class DesignTimeDbContextFactory<T> : IDesignTimeDbContextFactory<T> where T : DbContext { public T CreateDbContext(string[] args) { IConfigurationRoot configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json") .Build(); var builder = new DbContextOptionsBuilder<T>(); var connectionString = configuration.GetConnectionString("DefaultConnection"); builder.UseSqlServer(connectionString); var dbContext = (T)Activator.CreateInstance( typeof(T), builder.Options); return dbContext; } } 

 internal class ContextDesignTimeDbContextFactory : DesignTimeDbContextFactory<Context> { } 

并在WebApi项目

 internal class ApiContextDesignTimeDbContextFactory : DesignTimeDbContextFactory<ApiContext> { } 

我想用entity framework核心迁移。 当我跑步

 dotnet ef migrations add InitialCreate 

在ApiProject(使用CMD)我得到

发现多个DbContext。 指定使用哪一个。 使用PowerShell命令的“-Context”参数和dotnet命令的“–context”参数。

你能告诉我如何处理Asp.Net Core 2.0和EntityFrameworkCore 2和docker上的两个上下文的数据库迁移。

你应该指定你使用的DbContext:

 dotnet ef migrations add ArbitraryMigrationName -c YourDbContextName