How change connection string runtime in ef core?

Discussion of open issues, suggestions and bugs regarding Entity Framework support in ADO.NET Data providers
Post Reply
keng
Posts: 6
Joined: Wed 01 Feb 2017 02:33

How change connection string runtime in ef core?

Post by keng » Fri 18 Aug 2017 05:58

Hi
How change connection string runtime in ef core?
I try to change connection string on runtime but data no change.

Code: Select all

public string ConnectionString { get; set; }
        public EFDbContext() :
            base()
        {
            OnCreated();
        }

        public EFDbContext(DbContextOptions<EFDbContext> options) :
            base(options)
        {
            OnCreated();
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseMySql(ConnectionString);
            CustomizeConfiguration(ref optionsBuilder);
            base.OnConfiguring(optionsBuilder);
        }

Please help me.

Shalex
Site Admin
Posts: 8239
Joined: Thu 14 Aug 2008 12:44

Re: How change connection string runtime in ef core?

Post by Shalex » Fri 18 Aug 2017 09:16

You can set connection string used by EF Core context in runtime:

Code: Select all

        var optionsBuilder = new DbContextOptionsBuilder<ModelContext>();
        optionsBuilder.UseMySql(@"User Id=root;Password=root;Host=db;Port=3311;Database=aaaa;");
        var context = new ModelContext(optionsBuilder.Options);
        var result = context.Depts.ToList();

        var optionsBuilder2 = new DbContextOptionsBuilder<ModelContext>();
        optionsBuilder2.UseMySql(@"User Id=root;Password=root;Host=db;Port=3312;Database=bbbb;");
        var context2 = new ModelContext(optionsBuilder2.Options);
        var result2 = context2.Depts.ToList();
There is a known issue: our EF Core template currently generates the following code

Code: Select all

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseMySql(@"...");
            CustomizeConfiguration(ref optionsBuilder);
            base.OnConfiguring(optionsBuilder);
        }
which overrides connection string set via your own code. We will fix code generation in this way

Code: Select all

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.Options.Extensions.OfType<RelationalOptionsExtension>().Any(ext => !string.IsNullOrEmpty(ext.ConnectionString) || ext.Connection != null))
                optionsBuilder.UseMySql(@"...");

            CustomizeConfiguration(ref optionsBuilder);
            base.OnConfiguring(optionsBuilder);
        }
and will notify you when the change is included in a predefined EF Core template.

Shalex
Site Admin
Posts: 8239
Joined: Thu 14 Aug 2008 12:44

Re: How change connection string runtime in ef core?

Post by Shalex » Fri 08 Sep 2017 13:13

The issue is fixed in the new (8.9.980) build of dotConnect for MySQL.

Post Reply