efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Migrations of multiple DbContexts with different connection strings

Open alexanium-dev opened this issue 10 years ago • 15 comments

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"

alexanium-dev avatar Apr 06 '16 16:04 alexanium-dev

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 avatar Apr 07 '16 22:04 aschlesener

@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?

ajcvickers avatar Apr 08 '16 17:04 ajcvickers

Following on from this, the fix is likely to be to use generic DbContext<TContext> parameters in the DbContext classes.

ajcvickers avatar Apr 08 '16 22:04 ajcvickers

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

mikes-gh avatar Apr 11 '16 14:04 mikes-gh

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 avatar Apr 13 '16 14:04 mikes-gh

@mikes-gh Can you post the constructors for ApplicationDbContext and TradingCompanyDbContext?

ajcvickers avatar Apr 13 '16 15:04 ajcvickers

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

mikes-gh avatar Apr 13 '16 16:04 mikes-gh

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 avatar Apr 13 '16 16:04 mikes-gh

@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."

ajcvickers avatar Apr 13 '16 16:04 ajcvickers

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 avatar Apr 13 '16 17:04 mikes-gh

@mikes-gh The generic DbContextOptions inherits from the non-generic DbContextOptions, so yes you can.

That line does not need to change,

ajcvickers avatar Apr 13 '16 17:04 ajcvickers

@ajcvickers Thanks for your help. I confirm changing the contructors to generic options with existing code staying the same works.

mikes-gh avatar Apr 14 '16 07:04 mikes-gh

Reactivating this to get a chance to make the changes in RC2. cc @natemcmaster @rowanmiller @ajcvickers

divega avatar Apr 26 '16 18:04 divega

Changes for release branch are already included in https://github.com/aspnet/EntityFramework/pull/5183, so assigning this to @natemcmaster.

divega avatar Apr 26 '16 18:04 divega

Fixed in #5186

natemcmaster avatar Apr 26 '16 21:04 natemcmaster