2

我正在玩新的ASP.NET 5 beta 8,并遇到了麻烦,当我有两个dbcontext。ASP.NET 5多个dbcontext问题

我有以下项目结构。

-Data(Identity 3 db with other entities) 
-Resources (Contains a db with translations) 
-WebApp 

剥夺了一些代码Startup.cs在Web应用程序

public void ConfigureServices(IServiceCollection services) 
{ 
     services.AddEntityFramework() 
      .AddSqlServer() 
      .AddDbContext<DatabaseContext>(opt => opt.UseSqlServer(Configuration["Data:MainDb:ConnectionString"])); 

     services.AddIdentity<ApplicationUser, IdentityRole>() 
      .AddEntityFrameworkStores<DatabaseContext>() 
      .AddDefaultTokenProviders(); 

     services.AddEntityFramework() 
      .AddSqlServer() 
      .AddDbContext<ResourceDbContext>(opt => opt.UseSqlServer(Configuration["Data:Resources:ConnectionString"])); 

     services.AddTransient<IResourceDbContext, ResourceDbContext>(); 
     services.AddTransient<IDatabaseContext, DatabaseContext>(); 
} 

在这两个ResourceDbContext和DatabaseContext我下面

public ResourceDbContext(DbContextOptions options) : base(options) 
    { 
     _connectionString = ((SqlServerOptionsExtension)options.Extensions.First()).ConnectionString; 
    } 


    protected override void OnConfiguring(DbContextOptionsBuilder options) 
    { 
     options.UseSqlServer(_connectionString); 
    } 

然而,当我从appsettings.json阅读我的ConnectionStrings我在ConfigureServices中收到了正确的值。但DbContextOptions只包含最新加载的值,在这种情况下是Resources的连接字符串。因此,这两个dbcontext建立到资源数据库的连接。

我无法找到任何关于此的信息。

回答

3

所有你需要做的是表明DbContextOptions是一个泛型类型:

public ResourceDbContext(DbContextOptions<ResourceDbContext> options) : base(options) 
{ 

} 

现在依赖注入系统可以发现此刻的右依赖性(DbContextOptions options)它创建ResourceDbContext并将其注入到构造。

See implementation AddDbContext method


对于米罗斯拉夫Siska:

public class GetHabitsIdentity: IdentityDbContext<GetHabitsUser, IdentityRole, string> where TUser : IdentityUser 
{ 
    public GetHabitsIdentity(DbContextOptions<GetHabitsIdentity> options) 
     :base(options) 
    { 

    }   
} 
+0

太棒了,现在工作正常! – user1619493

0

您可以使用不同的coonection串EF。我没有看到你的代码中的错误。我用这个设置。

services.AddEntityFramework() 
      .AddSqlServer() 
      .AddDbContext<ApplicationDbContext>(options => 
       options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"])); 
     services.AddEntityFramework() 
      .AddSqlServer() 
      .AddDbContext<ApplicationContext>(options => 
       options.UseSqlServer(Configuration["Data:OtherConnection:ConnectionString"])); 

在的DbContext类

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
{ 
    public DbSet<Localizations> Localizations { get; set; } 
    private string _connectionString; 

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
    { 
     _connectionString = ((SqlServerOptionsExtension)optionsBuilder.Options.Extensions.First()).ConnectionString; 
     Console.WriteLine($"ApplicationDbContext{_connectionString}"); 
    } 


public class ApplicationContext : DbContext 
{ 
    private string _connectionString; 

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
    { 
     _connectionString = ((SqlServerOptionsExtension)optionsBuilder.Options.Extensions.First()).ConnectionString; 
     Console.WriteLine($"ApplicationContext{_connectionString}"); 
    } 
} 
0

谢谢你很大的帮助!

完整的解决方案:

public class TenantDbContext : IdentityDbContext<ApplicationUser, IdentityRole, string> 
{ 
    private string _connectionString { get; set; } 

    public TenantDbContext(DbContextOptions<TenantDbContext> options) : base(options) 
    { 
     this._connectionString = "Connection String"; 
    } 

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
    { 
     optionsBuilder.UseSqlServer(_connectionString); 
    } 
} 

ASP.NET 5 DI用于注射的DbContext和的UserManager到控制器工作。现在可以登录并注册多个数据库......现在我只需要检查如何在这里注入连接字符串:this._connectionString =“Connection String”;但很简单...再次感谢您!