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

[release/9.0] Use the specified migrations assembly in validation #34777

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/EFCore.Design/Design/Internal/MigrationsOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,19 @@ private void EnsureMigrationsAssembly(IServiceProvider services)
var assemblyName = _assembly.GetName();
var options = services.GetRequiredService<IDbContextOptions>();
var contextType = services.GetRequiredService<ICurrentDbContext>().Context.GetType();
var migrationsAssemblyName = RelationalOptionsExtension.Extract(options).MigrationsAssembly
?? contextType.Assembly.GetName().Name;
if (assemblyName.Name != migrationsAssemblyName
&& assemblyName.FullName != migrationsAssemblyName)
var optionsExtension = RelationalOptionsExtension.Extract(options);
if (optionsExtension.MigrationsAssemblyObject == null
|| optionsExtension.MigrationsAssemblyObject != _assembly)
{
throw new OperationException(
DesignStrings.MigrationsAssemblyMismatch(assemblyName.Name, migrationsAssemblyName));
var migrationsAssemblyName = optionsExtension.MigrationsAssembly
?? optionsExtension.MigrationsAssemblyObject?.GetName().Name
?? contextType.Assembly.GetName().Name;
if (assemblyName.Name != migrationsAssemblyName
&& assemblyName.FullName != migrationsAssemblyName)
{
throw new OperationException(
DesignStrings.MigrationsAssemblyMismatch(assemblyName.Name, migrationsAssemblyName));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ public MigrationsAssembly(
{
_contextType = currentContext.Context.GetType();

var assemblyName = RelationalOptionsExtension.Extract(options).MigrationsAssembly;
var assemblyObject = RelationalOptionsExtension.Extract(options).MigrationsAssemblyObject;
var optionsExtension = RelationalOptionsExtension.Extract(options);
var assemblyName = optionsExtension.MigrationsAssembly;
var assemblyObject = optionsExtension.MigrationsAssemblyObject;

Assembly = assemblyName == null
? assemblyObject ?? _contextType.Assembly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,34 @@ public void Can_pass_null_args()
args: null);
}

[ConditionalFact]
public void Can_use_migrations_assembly()
{
// Even though newer versions of the tools will pass an empty array
// older versions of the tools can pass null args.
var assembly = MockAssembly.Create(typeof(AssemblyTestContext));
var migrationsAssembly = MockAssembly.Create();
AssemblyTestContext.MigrationsAssembly = migrationsAssembly;
var testOperations = new TestMigrationsOperations(
new TestOperationReporter(),
assembly,
assembly,
"projectDir",
"RootNamespace",
"C#",
nullable: false,
args: null);

testOperations.AddMigration("Test", null, null, null, dryRun: true);
}

private class TestContext : DbContext;

private class AssemblyTestContext : DbContext
{
public static Assembly MigrationsAssembly { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlServer(o => o.MigrationsAssembly(MigrationsAssembly));
}
}