Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not possible to asynchronously define schema name at runtime #20600

Closed
kewinbrand opened this issue Apr 11, 2020 · 2 comments
Closed

Not possible to asynchronously define schema name at runtime #20600

kewinbrand opened this issue Apr 11, 2020 · 2 comments

Comments

@kewinbrand
Copy link

Hey,

Say I have a system that uses a database that the data is separated by schema and the schemas name are discovered at runtime.
Currently what I do is inject a custom service in my migrations that discovers the correct schema (my migrations are applied at runtime for each customer when needed):

public partial class AddedPopulatioProperty : Migration
{
	
  private readonly ISchemaNameProvider _schemaNameProvider;
  
  public AddedPopulatioProperty(ISchemaNameProvider schemaNameProvider)
  {
  	_schemaNameProvider = schemaNameProvider;
  }
  
  protected override void Up(MigrationBuilder migrationBuilder)
  {
  	migrationBuilder.AddColumn<float>(
  		name: "Population",
  		table: "Cities",
  		nullable: true, schema: _schemaNameProvider.GetSchemaName());
  }
  
  protected override void Down(MigrationBuilder migrationBuilder)
  {
  	migrationBuilder.DropColumn(
  		name: "Population",
  		table: "Cities", schema: _schemaNameProvider.GetSchemaName());
  }
}

But this has a problem which is what if my GetSchemaName is async? Like making a call to a external service or getting it from a distributed cache?

Thank you so far.

@ajcvickers
Copy link
Contributor

/cc @bricelam

@bricelam
Copy link
Contributor

One (slightly ugly) option is to use a custom base type for migrations:

public abstract class MyMigrationBase : Migration
{
    public static string DefaultSchema { get; set; }
}

public partial class AddedPopulatioProperty : MyMigrationBase
{
  protected override void Up(MigrationBuilder migrationBuilder)
  {
  	migrationBuilder.AddColumn<float>(
  		name: "Population",
                schema: DefaultSchema,
  		table: "Cities",
  		nullable: true);
    }
)

And set it before migrating:

MyMigrationBase.DefaultSchema = "mySchema";
dbContext.Database.Migrate();

Duplicate of #10660 for injecting a service.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants