From a0952960288735cedd2d9ebc4e1078bcbffc7d26 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 27 Aug 2025 01:21:36 +0000 Subject: [PATCH 1/3] Initial plan From abfa78243b344bd5829cd2a661c7507956690d7d Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 27 Aug 2025 01:38:10 +0000 Subject: [PATCH 2/3] Fix AmbiguousMatchException in MigrationsAssembly when migration classes inherit DbContextAttribute Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com> --- .../Migrations/Internal/MigrationsAssembly.cs | 4 +- .../Internal/MigrationsAssemblyTest.cs | 41 +++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/EFCore.Relational/Migrations/Internal/MigrationsAssembly.cs b/src/EFCore.Relational/Migrations/Internal/MigrationsAssembly.cs index 9b0c75444cd..277c0ca71a2 100644 --- a/src/EFCore.Relational/Migrations/Internal/MigrationsAssembly.cs +++ b/src/EFCore.Relational/Migrations/Internal/MigrationsAssembly.cs @@ -60,7 +60,7 @@ IReadOnlyDictionary Create() var items = from t in Assembly.GetConstructibleTypes() where t.IsSubclassOf(typeof(Migration)) - && t.GetCustomAttribute()?.ContextType == _contextType + && t.GetCustomAttribute(inherit: false)?.ContextType == _contextType let id = t.GetCustomAttribute()?.Id orderby id select (id, t); @@ -94,7 +94,7 @@ public virtual ModelSnapshot? ModelSnapshot => _modelSnapshot ??= (from t in Assembly.GetConstructibleTypes() where t.IsSubclassOf(typeof(ModelSnapshot)) - && t.GetCustomAttribute()?.ContextType == _contextType + && t.GetCustomAttribute(inherit: false)?.ContextType == _contextType select (ModelSnapshot)Activator.CreateInstance(t.AsType())!) .FirstOrDefault(); diff --git a/test/EFCore.Relational.Tests/Migrations/Internal/MigrationsAssemblyTest.cs b/test/EFCore.Relational.Tests/Migrations/Internal/MigrationsAssemblyTest.cs index cbc97dcd3d2..82ebf1a760a 100644 --- a/test/EFCore.Relational.Tests/Migrations/Internal/MigrationsAssemblyTest.cs +++ b/test/EFCore.Relational.Tests/Migrations/Internal/MigrationsAssemblyTest.cs @@ -84,4 +84,45 @@ protected override void Up(MigrationBuilder migrationBuilder) { } } + + [ConditionalFact] + public void Migrations_handles_inherited_DbContextAttribute() + { + var assembly = CreateInheritedMigrationsAssembly(); + + // This should not throw AmbiguousMatchException + var result = assembly.Migrations; + + Assert.Single(result); + Assert.Contains(result, t => t.Key == "20150302103200_InheritedMigration"); + } + + private IMigrationsAssembly CreateInheritedMigrationsAssembly() + => new MigrationsAssembly( + new CurrentDbContext(new DerivedContext()), + new DbContextOptions( + new Dictionary + { + { typeof(FakeRelationalOptionsExtension), new FakeRelationalOptionsExtension() } + }), + new MigrationsIdGenerator(), + new FakeDiagnosticsLogger()); + + private class DerivedContext : Context; + + [DbContext(typeof(Context)), Migration("20150302103200_BaseMigration")] + private class BaseMigration : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + } + } + + [DbContext(typeof(DerivedContext)), Migration("20150302103200_InheritedMigration")] + private class InheritedMigration : BaseMigration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + } + } } From 47cd68fa0a095bbbdb68f35bd80474643aa98c2e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 27 Aug 2025 22:42:29 +0000 Subject: [PATCH 3/3] Adjust test to account for new BaseMigration class Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com> --- .../Migrations/Internal/MigrationsAssemblyTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/EFCore.Relational.Tests/Migrations/Internal/MigrationsAssemblyTest.cs b/test/EFCore.Relational.Tests/Migrations/Internal/MigrationsAssemblyTest.cs index 82ebf1a760a..bde37108115 100644 --- a/test/EFCore.Relational.Tests/Migrations/Internal/MigrationsAssemblyTest.cs +++ b/test/EFCore.Relational.Tests/Migrations/Internal/MigrationsAssemblyTest.cs @@ -40,7 +40,7 @@ public void Migrations_ignores_the_unattributed() var result = assembly.Migrations; - Assert.Equal(2, result.Count); + Assert.Equal(3, result.Count); Assert.DoesNotContain(result, t => t.GetType() == typeof(MigrationWithoutAttribute)); Assert.Equal( RelationalResources.LogMigrationAttributeMissingWarning(logger).GenerateMessage(nameof(MigrationWithoutAttribute)),