代码改变世界

EF Core 多 DbContext 迁移

2020-03-19 15:17  Dorisoy  阅读(280)  评论(0编辑  收藏  举报
public class FirstDbContext : DbContext
    {
        public FirstDbContext(DbContextOptions<FirstDbContext> options)
            : base(options)
        { }
        public DbSet<xx> xxxs{ get; set; }
        public DbSet<xx> xxxs{ get; set; }
    }

public class SecondDbContext : DbContext
    {
        public SecondDbContext (DbContextOptions<FirstDbContext> options)
            : base(options)
        { }
        public DbSet<xx> xxxs{ get; set; }
        public DbSet<xx> xxxs{ get; set; }
    }


public void ConfigureServices(IServiceCollection services)
 {
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        var connection = @"xxx"; 
        services.AddDbContext<FirstDbContext> 
            (options => options.UseSqlServer(connection)); 
        var secondDbconnection = @"xxx"; 
        services.AddDbContext<SecondDbContext> 
            (options => options.UseSqlServer(secondDbconnection)); 
    }
Add-Migration InitialCreate -Context FirstDbContext -OutputDir Migrations\FirstDbContextMigrations
Update-Database -Context FirstDbContext