我正在使用新的ASP.NET 5 beta 8,当我有两个dbcontext时遇到麻烦。
我有以下项目结构。
-Data(Identity 3 db with other entities)
-Resources (Contains a db with translations)
-WebApp
在WebApp
中的Startup.cs中删除了一些代码 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读取连接字符串时,我在ConfigureServices中收到了正确的值。但是DbContextOptions只包含最新加载的值,在本例中是Resources的连接字符串。所以dbcontext都建立了与Resource db的连接。
我无法找到有关此内容的任何信息。
答案 0 :(得分:3)
您需要做的就是表明DbContextOptions是泛型类型:
public ResourceDbContext(DbContextOptions<ResourceDbContext> options) : base(options)
{
}
现在,依赖注入系统可以在创建ResourceDbContext并将其注入构造函数时找到正确的依赖项(DbContextOptions options
)。
See implementation AddDbContext method
Miroslav Siska:
public class GetHabitsIdentity: IdentityDbContext<GetHabitsUser, IdentityRole, string> where TUser : IdentityUser
{
public GetHabitsIdentity(DbContextOptions<GetHabitsIdentity> options)
:base(options)
{
}
}
答案 1 :(得分:0)
您可以将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}");
}
}
答案 2 :(得分: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”;但这很简单......再次感谢你!