Description
Bug description
I created inherit DbContexts to handle some tests, all works well using ef 8, but today I upgrade it to ef 9.0.1 and throws this error:
System.Reflection.AmbiguousMatchException
Multiple custom attributes of the same type 'Microsoft.EntityFrameworkCore.Infrastructure.DbContextAttribute' found.
at System.Attribute.GetCustomAttribute(MemberInfo element, Type attributeType, Boolean inherit)
at System.Reflection.CustomAttributeExtensions.GetCustomAttribute[T](MemberInfo element)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsAssembly.<get_ModelSnapshot>b__9_0(TypeInfo t)
...
My suspect is that the MigrationsAssembly is getting all inherits attributes of type DbContextAttribute instead of taking only the one related to the actual class, I deleted the DbContextAttribute in generated migration partial class and all works ok again.
Your code
// generated migration class
[DbContext(typeof(MyDbContext))] // this is removed in order to works ok
[Migration("FirstMigration")]
partial class FirstMigration // ...
// my extended class
[DbContext(typeof(MyDbContextExtended))]
[Migration("FirstMigrationExtended")]
class FirstMigrationExtended : FirstMigration // ... extra info added to the migration
Stack traces
System.Reflection.AmbiguousMatchException
Multiple custom attributes of the same type 'Microsoft.EntityFrameworkCore.Infrastructure.DbContextAttribute' found.
at System.Attribute.GetCustomAttribute(MemberInfo element, Type attributeType, Boolean inherit)
at System.Reflection.CustomAttributeExtensions.GetCustomAttribute[T](MemberInfo element)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsAssembly.<get_ModelSnapshot>b__9_0(TypeInfo t)
at System.Linq.Enumerable.ArrayWhereSelectIterator`2.TryGetFirst(ReadOnlySpan`1 source, Func`2 predicate, Func`2 selector, Boolean& found)
at System.Linq.Enumerable.ArrayWhereSelectIterator`2.TryGetFirst(Boolean& found)
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsAssembly.get_ModelSnapshot()
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.ValidateMigrations(Boolean useTransaction)
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.MigrateAsync(String targetMigration, CancellationToken cancellationToken)
Verbose output
[21:13:56 DBG] An 'IServiceProvider' was created for internal use by Entity Framework.
[21:13:59 VRB] Database deletion...
[21:14:00 DBG] Entity Framework Core 9.0.1 initialized 'TestDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer:9.0.1' with options: CommandTimeout=1800 MigrationsAssembly=Test.Utilities.Db.SqlServer EngineType=SqlServer DetailedErrorsEnabled using HierarchyId
[21:14:00 DBG] Creating DbConnection.
[21:14:00 DBG] Created DbConnection. (18ms).
[21:14:00 DBG] Opening connection to database 'Test.Integration' on server 'tcp:.,1433'.
[21:14:01 DBG] An error occurred using the connection to database 'Test.Integration' on server 'tcp:.,1433'.
[21:14:01 VRB] Database deleted
[21:14:01 VRB] Database migration...
[21:14:04 DBG] 'TestDbContext' disposed.
[21:14:04 DBG] Disposing connection to database 'Entities.Test.Integration' on server 'tcp:.,1433'.
[21:14:04 DBG] Disposed connection to database '' on server '' (2ms).
EF Core version
9,0,1
Database provider
Microsoft.EntityFrameworkCore.SqlServer
Target framework
.net 9.0
Operating system
Windows 11
IDE
Visual studio 2022 17.12.4