0

I want to be able to change the connectionstring of IDesignTimeDbContextFactory in run time.

public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<erp_colombiaDbContext>
{
    public erp_colombiaDbContext CreateDbContext(string[] args) 
    {
        var options = new DbContextOptionsBuilder<erp_colombiaDbContext>().UseMySql(
                @"SECRECT CONNECTION STRING SHOULD BE PARAMETER",
                optionsBuilder => optionsBuilder.MigrationsAssembly(typeof(DesignTimeDbContextFactory).Assembly.FullName))
            .Options;

        return new erp_colombiaDbContext(options);
    }

When I am generating the data here I have some tables that should be in a diffrent database

public class erp_colombiaDbContext : IdentityDbContext<Employee, Entities.Type, ulong>
{

    public erp_colombiaDbContext(DbContextOptions options) : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        //For exemple the family should be in database 2
        builder.Entity<Family>().HasIndex(t => t.FamilyName).IsUnique(); 
        //And the news author should be in database 1
        builder.Entity<NewsAuthor>().HasKey(t => new { t.NewsId, t.EmployeeId });
    }
}
J.C
  • 632
  • 1
  • 10
  • 41
  • connect multiple database via ef core? if so, [this answer](https://stackoverflow.com/questions/58123230/connect-multiple-databases-to-net-core-project-via-entity-framework-core) may help. – Tiny Wang Sep 12 '22 at 03:11

2 Answers2

1

add connection string in appsettings.json file

{
  "ConnectionStrings": {
    "DefaultConnection": "<your server name>Catalog=GeeksStore;
        Integrated Security=True;Connect Timeout=30;Encrypt=False;
        TrustServerCertificate=False;ApplicationIntent=ReadWrite;
        MultiSubnetFailover=False"
  }  
}

and in create context method

public class DesignTimeDbContextFactory : 
        IDesignTimeDbContextFactory<StoreContext>
   {
        public StoreContext CreateDbContext(string[] args)
        {
            IConfigurationRoot configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .Build();
            var builder = new DbContextOptionsBuilder<StoreContext>();
            var connectionString = configuration.GetConnectionString("DefaultConnection");
            builder.UseSqlServer(connectionString);
            return new StoreContext(builder.Options);
        }
    }
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Laxmikant
  • 588
  • 4
  • 11
  • Thank you for your help I have some questions what do I do in OnModelCreating when I want to change the defaultConection? Also your link is not working for me. – J.C Sep 12 '22 at 02:13
  • The link is working now. However I am still unsure how to change the connection in OnModelCreating can you show me how it is the only part that i am usure – J.C Sep 12 '22 at 13:15
  • @J.C you do not require to do anything in the ModelCreating method. Implement DesignTimeDbContextFactory interface in one class and update your database using Update-Database -verbose as shown in link. Check out the code attached with link – Laxmikant Sep 12 '22 at 22:39
  • But in your example both tables are in Geeksstore? I want to have NewsAuthor in a diffrent "schema" P.S I am using the schema definition in mysql I belive that in mssql it is called a database very confusing. – J.C Sep 13 '22 at 13:19
  • 1
    different schema or different database? – Laxmikant Sep 14 '22 at 16:06
  • diffrent schema – J.C Sep 14 '22 at 20:55
1

you can update your OnModelCreating method and use Fluent API to configure schema for each table

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<model name1>().ToTable("my sql table name1", "schema name1");
    modelBuilder.Entity<model name2>().ToTable("my sql table name2", "schema name2");       
}
Laxmikant
  • 588
  • 4
  • 11
  • Yes, thank you very much it is almost what I want to have. Now I am able to populate the table in the schema that I want. However, where do I specify in a model witch schema he should use. Because for the moment being it still creates the table in the catalog that is selected in IDesignTimeDbContextFactory. – J.C Sep 15 '22 at 15:12