我正在玩新的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建立到资源数据库的连接。
我无法找到任何关于此的信息。
太棒了,现在工作正常! – user1619493