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

Allow Migrations assemble to be loaded from different assemblyLoadContexts #32461

Closed
davidnajar opened this issue Nov 30, 2023 · 1 comment · Fixed by #32473
Closed

Allow Migrations assemble to be loaded from different assemblyLoadContexts #32461

davidnajar opened this issue Nov 30, 2023 · 1 comment · Fixed by #32473
Labels
area-migrations closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. community-contribution customer-reported type-enhancement

Comments

@davidnajar
Copy link
Contributor

davidnajar commented Nov 30, 2023

Currently, when developing a plugin-based mechanism where each plugin is loaded from an isolated Assembly Load Context (for instance using DotNetCorePlugins the migrations can't be loaded from a referenced asembly of the plugin. They need to stay at the same type were the plugin type is defined.
Currently there are two options for defining the assembly where migrations are located:
builder.UseSqlServer(_connectionString, b => b.MigrationsAssembly(migrationsAssembly);

being migrationsAssembly a plain string, or not specifying anything at all.

Internally, this is handled in the following way:
MigrationsAssembly.cs

 public MigrationsAssembly(
        ICurrentDbContext currentContext,
        IDbContextOptions options,
        IMigrationsIdGenerator idGenerator,
        IDiagnosticsLogger<DbLoggerCategory.Migrations> logger)
    {
        _contextType = currentContext.Context.GetType();

        var assemblyName = RelationalOptionsExtension.Extract(options).MigrationsAssembly;
        Assembly = assemblyName == null
            ? _contextType.Assembly
            : Assembly.Load(new AssemblyName(assemblyName));

        _idGenerator = idGenerator;
        _logger = logger;
    }

Note the condition: if there is not assemblyName specified it will use the same assembly where the context is defined. Otherwise will use Assembly.Load to load the assembly specified by name.

This does not work when the plugins are loaded from separate assembly load context, as the Assembly.Load is only loading assemblies from the AssemblyLoadContext.Default.

Ideally, when specifying the migrations assembly, one could give directly the Assembly object, leveraging the responsibility of how to load the assembly to the caller, and not make assumptions like the one done with the Assembly.Load. There might be an overload to the

public virtual TBuilder MigrationsAssembly(string? assemblyName)
        => WithOption(e => (TExtension)e.WithMigrationsAssembly(Check.NullButNotEmpty(assemblyName, nameof(assemblyName))));

accepting an AssemblyObject itself

davidnajar pushed a commit to davidnajar/efcore that referenced this issue Nov 30, 2023
davidnajar pushed a commit to davidnajar/efcore that referenced this issue Nov 30, 2023
        - Allow to specify a migrations assembly using an Assembly type apart from an assemlyName

        Fixes dotnet#32461
davidnajar added a commit to davidnajar/efcore that referenced this issue Dec 1, 2023
        - Allow to specify a migrations assembly using an Assembly type apart from an assemlyName

 Fixes dotnet#32461
@ajcvickers
Copy link
Member

Note from triage: we discussed this and it sounds reasonable.

@ajcvickers ajcvickers added this to the 9.0.0 milestone Dec 7, 2023
@ajcvickers ajcvickers added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. community-contribution labels Dec 7, 2023
ajcvickers pushed a commit that referenced this issue Dec 7, 2023
- Allow to specify a migrations assembly using an Assembly type apart from an assemlyName

 Fixes #32461
@ajcvickers ajcvickers modified the milestones: 9.0.0, 9.0.0-preview1 Jan 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-migrations closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. community-contribution customer-reported type-enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants