dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.61k stars 3.14k forks source link

Support for a Single Project with Multiple Provider Migrations - Edge Case/Issue/Request #34197

Open anewton opened 1 month ago

anewton commented 1 month ago

Support for a Single Project with Multiple Provider Migrations - Edge Case/Issue/Request

Sample project to reproduce on my repo here: Single Project with Multiple Provider Migrations Issue - Entity Framework

Permalink to dotnet EF source code method not working as expected: efcore > src > EFCore.Relational > Migrations > Internal > Migrator.cs

public virtual void Migrate(string? targetMigration = null)

This project attempts to illustrate an edge case that makes separate class library projects a requirement when multiple database providers are used. There seems to be an error in an internal Migrate method that ignores any target migration id when supplied. Noting that this is all undocumented and not supported. It would be very nice to have support for the scenario described.

Code to retrieve the IMigrator implementation. Similar to EF source code that does the same thing.

    private static IMigrator GetMigratorService(DbContext dbContext)
    {
        DatabaseFacade databaseFacade = dbContext.Database;
        IInfrastructure<IServiceProvider> serviceProvider = databaseFacade;
        IMigrator service = serviceProvider.Instance.GetService<IMigrator>();
        return service ?? throw new InvalidOperationException(RelationalStrings.RelationalNotInUse);
    }

Documentation for the supported feature can be found here, Microsoft - Migrations with Multiple Providers, and here, Microsoft - Using a Separate Migrations Project.

Proposed solution:

Make a public method to allow running a migration by a specific target migration id. Or, fix the internal method that already has this parameter but seems not to work as expected. Based on the method name and parameter name.

Provider and version information

EF Core version: 8.0.6 or 8.0.7 Entity Framework Core .NET Command-line Tools: 8.0.6 Database provider: Microsoft.EntityFrameworkCore.SqlServer or Microsoft.Data.Sqlite Target framework: .NET 8.0 Operating system: Windows 11 Pro IDE: Visual Studio 2022 17.10.3

ajcvickers commented 1 month ago

Removing from the backlog because not triaged.