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

Multiple custom attributes of the same type 'Microsoft.EntityFrameworkCore.Infrastructure.DbContextAttribute' found. #35590

Open
chrisdreams13 opened this issue Feb 6, 2025 · 0 comments

Comments

@chrisdreams13
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants