Migrations of multiple DbContexts with different connection strings
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<FirstDbContext>(options =>
options.UseSqlServer(FIRST_CONNECTION_STRING))
.AddDbContext<SecondDbContext>(options =>
options.UseSqlServer(SECOND_CONNECTION_STRING));
dnx ef database update -c FirstDbContext -v writes changes to a database from the second connection string
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<FirstDbContext>(options =>
options.UseSqlServer(FIRST_CONNECTION_STRING));
//.AddDbContext<SecondDbContext>(options =>
// options.UseSqlServer(SECOND_CONNECTION_STRING));
dnx ef database update -c FirstDbContext -v writes changes to a database from the first connection string as expected
These observations from "dnx-1.0.0-rc1-update1"
We are experiencing the same issue (although not related to migrations), where adding a second dbcontext with a different connection string overwrites the first connection string - so both the first and the second dbcontexts use the second connection string.
Is there any workaround for this issue until the bug is fixed?
@aschlesener @newsash Couple of questions:
- Are you using current bits (RC2) or latest released bits (RC1)?
- Can you post what the constructors for your classes derived from DbContext looks like? In particular, I am interested if they use generic or non-generic DbContextOptions objects?
Following on from this, the fix is likely to be to use generic DbContext<TContext> parameters in the DbContext classes.
My startup is
services
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["Data:default:ConnectionString"]))
.AddDbContext<TradingCompanyDbContext>(options =>
options.UseSqlServer(Configuration["Data:default:ConnectionString"]));
I had to remove the second call to AddDbContext and use AddScoped and OnConfiguring instead to get it to work.
A short sample of how we are meant to do it now would be a great help
ping @ajcvickers . Sorry to ask again ( know you guys busy with rc2) A few lines of code on how multiple AddDbContext should be done would be much appreciated by me and would be helpful for others.
@mikes-gh Can you post the constructors for ApplicationDbContext and TradingCompanyDbContext?
I removed the constructor for the tradingcompanycontext and used on configuring and addscoped straight di
My constructor for applicationdbcontext is
namespace Reports2.Models
{
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
}
}
}
I see you said to use generics on contructor but I didn't have success on that as it need to call base which needs a different type again
for completeness
namespace Reports2.Models
{
public class TradingCompanyDbContext:ReportingDbContext
{
private readonly IConfiguration _config;
private readonly IHttpContextAccessor _httpContextAccessor;
public TradingCompanyDbContext(IConfiguration config, IHttpContextAccessor httpContextAccessor)
{
_config = config;
_httpContextAccessor = httpContextAccessor;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var tradingCompany = _httpContextAccessor.HttpContext.GetRouteValue("tradingCompany");
if (tradingCompany != null)
{
var connectionString = _config["Data:" + tradingCompany + ":ConnectionString"];
optionsBuilder.UseSqlServer(connectionString);
}
}
}
}
@mikes-gh The constructor for ApplicationDbContext should take a DbContextOptions<ApplicationDbContext> and the constructor for TradingCompanyDbContext should take a DbContextOptions<TradingCompanyDbContext>.
Can you further explain what you mean by, "I see you said to use generics on contructor but I didn't have success on that as it need to call base which needs a different type again."
ApplicationDbContext inherits from IndentityDbContext so I cant just pass the generic options down the line can I with base(options)?
Also what would the AddDbContext call look like fro ApplicationDbContext
AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["Data:default:ConnectionString"]))
How would I change the above Apologies if it is simple. I'm not too familiar with generics in general.
@mikes-gh The generic DbContextOptions inherits from the non-generic DbContextOptions, so yes you can.
That line does not need to change,
@ajcvickers Thanks for your help. I confirm changing the contructors to generic options with existing code staying the same works.
Reactivating this to get a chance to make the changes in RC2. cc @natemcmaster @rowanmiller @ajcvickers
Changes for release branch are already included in https://github.com/aspnet/EntityFramework/pull/5183, so assigning this to @natemcmaster.
Fixed in #5186