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」;但很簡單...再次感謝您!