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

Using separate migrations assemblies in example doesn't work #4318

Open
replaysMike opened this issue Apr 10, 2023 · 2 comments
Open

Using separate migrations assemblies in example doesn't work #4318

replaysMike opened this issue Apr 10, 2023 · 2 comments
Assignees
Milestone

Comments

@replaysMike
Copy link

The page documents how to create a project using a single DbContext with multiple providers using external migration assemblies, however this doesn't seem to work.

Providing the migrations assembly on creation of a migration:

dotnet ef migrations add InitialCreate --project Data\App.Data.Migrations.Sqlite --startup-project Data\App.Data --context AppContext

Yields an error:
"Could not load assembly 'App.Data.Migrations.Sqlite'. Ensure it is referenced by the startup project 'App.Data'."

// some factory setup method
switch (_storageProviderConfiguration.Provider.ToLower())
{
    case "sqlite":
        optionsBuilder.UseSqlite(connectionString, x => x.MigrationsAssembly("App.Data.Migrations.Sqlite"));
        break;
    case "sqlserver":
        optionsBuilder.UseSqlServer(connectionString, x => x.MigrationsAssembly("App.Data.Migrations.SqlServer"));
        break;
    case "postgresql":
        optionsBuilder.UseNpgsql(connectionString, x => x.MigrationsAssembly("App.Data.Migrations.Postgresql"));
        break;
    case "mysql":
        var serverVersion = ServerVersion.AutoDetect(connectionString);
        optionsBuilder.UseMySql(connectionString, serverVersion, x => x.MigrationsAssembly("App.Data.Migrations.MySql"));
        break;
    default:
        throw new NotSupportedException($"Unsupported database provider '{_storageProviderConfiguration.Provider}'");
}

The assembly hosting the context doesn't have references to the migration assemblies, and can't because it would create a circular dependency. The migrations assemblies require a reference to the context in order to function.

Am I missing something here?


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

@replaysMike
Copy link
Author

replaysMike commented Apr 10, 2023

A temporary workaround could be to copy the migration assembly dll's to the App.Data output folder after build, but that's not ideal at all. A better solution would be giving it a way to resolve those assemblies.

Looking at the Two project or Three project examples I'm not quite sure how it could work with no reference to the assemblies.

The Three Project example looked more promising, since it has a reference to the migrations assemblies as well as the DbContext. However, the DbContext cannot be found.

dotnet ef migrations add InitialCreate --project Data\App.Data.Migrations.Sqlite --startup-project Data\App.Data.Stub --context AppContext

No DbContext named 'AppContext' was found.

@ajcvickers
Copy link
Member

Note for triage: it would be really useful to have an actual sample solution for this so that I'm not left trying to stitch things together for multiple places.

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

2 participants