Entity Framework Core multiple connection strings on same DBContext?

前端 未结 3 999
误落风尘
误落风尘 2020-12-30 07:03

I have an Asp.Net Core app with Entity Framework Core that I initialize as follows:

services.AddDbContext(options => 
                 


        
3条回答
  •  我在风中等你
    2020-12-30 07:40

    You'll need two DbContexts.

    public class BloggingContext : DbContext
    {
        public DbSet Blogs { get; set; }
        public DbSet Posts { get; set; }
    }
    
    public class MyBloggingContext : BloggingContext
    {
    
    }
    
    public class MyBackupBloggingContext : BloggingContext
    {
    
    }
    

    And you can register them like this:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    
        services.AddDbContext(options =>
            options.UseSqlServer(Configuration.GetConnectionString("BackupConnection")));
    
    }
    

提交回复
热议问题