Skip to content

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

Closed

Description

@replaysMike

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.

Activity

  1. replaysMike commented on Apr 10, 2023

    @replaysMike
    Author

    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.

  2. ajcvickers commented on Apr 17, 2023

    @ajcvickers
    Contributor

    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.

  3. added this to the Backlog milestone on Apr 27, 2023
  4. self-assigned this
    on Apr 27, 2023
  5. mKenfenheuer commented on Jul 17, 2024

    @mKenfenheuer

    I have had the same problem, turned out after removing the dot in the assembly name it worked!

    Before KCloud.Migrations.Sqlite
    After SqliteMigrations

    Now it works.

  6. mKenfenheuer commented on Jul 17, 2024

    @mKenfenheuer

    @ajcvickers Just checked with the sample code TwoProjectMigrations.

    If you take the project as is, it works just fine.
    If you add a dot somewhere in the project names (e.g. Sqlite.Migrations or SqlServer.Migrations) it will not work anymore.
    Please refer to the patch for further details: patch.txt

  7. removed their assignment
    on Aug 31, 2024
  8. added a commit that references this issue on Aug 11, 2026
    bb42937
  9. modified the milestones: Backlog, 11.0.0 on Aug 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions