-
Notifications
You must be signed in to change notification settings - Fork 3.4k
[release/10.0] Fix migration discovery when [DbContext] is only on base class #37437
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/10.0] Fix migration discovery when [DbContext] is only on base class #37437
Conversation
Co-authored-by: AndriySvyryd <[email protected]>
|
@copilot Update progress by rewriting the PR summary using this template: |
... Updated the PR description to follow the standard template format. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR fixes a regression in EF Core 10 RC2 where migrations are no longer discovered when the [DbContext] attribute is placed only on a base migration class rather than on the concrete migration classes. The fix manually walks the type inheritance chain to find the first DbContextAttribute, avoiding AmbiguousMatchException while restoring support for inherited attributes.
Key Changes:
- Adds a
GetDbContextTypehelper method that manually traverses the inheritance hierarchy to find theDbContextAttribute - Replaces direct
GetCustomAttributecalls with the new helper method in both migration and model snapshot discovery - Adds comprehensive test coverage for the scenario where the attribute is only on the base class
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/EFCore.Relational/Migrations/Internal/MigrationsAssembly.cs | Introduces GetDbContextType helper method that walks the inheritance chain to find DbContextAttribute, replacing direct GetCustomAttribute calls in migration and model snapshot discovery |
| test/EFCore.Relational.Tests/Migrations/Internal/MigrationsAssemblyTest.cs | Adds test case Migrations_finds_attribute_on_base_class_only to verify migrations are discovered when [DbContext] attribute is only on base class, with supporting test infrastructure |
|
cc @artl93 |
artl93
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Customer reported regression, but uncommon. How discoverable is the workaround?
|
The are no exceptions thrown, updating the database just prints a message saying that no migrations were found, so it would take some intuition in how the migrations infrastructure works and a guess for where the bug might be to find the workaround naturally. And since this is a regression then the person debugging this could be different from the person that created the base types for the migrations initially. |
|
Thanks - approved. Please state this about the workaround in the tactics mail. |
artl93
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See prior comment
Fixes #37431
Description
Migrations are no longer discovered if the
DbContextAttributeis placed on a base class rather than the concrete migration class.The fix manually walks the type inheritance chain to find the first
DbContextAttribute, which supports inheritance while avoidingAmbiguousMatchExceptionby preferring the most derived class's attribute.Customer impact
Customers using a base migration class with [DbContext] attribute on the base class and not on the derived classes cannot use migrations in EF Core 10 RC2. The migration discovery fails completely, preventing any migration operations from working.
Workaround: Customers must add the [DbContext] attribute to each individual migration class instead of using a base class pattern, which adds repetitive boilerplate code. However, There are no exceptions thrown, updating the database just prints a message saying that no migrations were found, so it would take some intuition in how the migrations infrastructure works and a guess for where the bug might be to find the workaround naturally. And since this is a regression then the person debugging this could be different from the person that created the base types for the migrations initially.
How found
Customer reported via npgsql/efcore.pg#3697. But the issue affects all database providers, not just Npgsql. However, this is not a common pattern as the migrations generated by EF do not derive from any class.
Regression
Yes, regression from EF Core 9.0. It was introduced in EF Core 10.0.0-rc.2 via #36666,
Testing
Test added
Risk
Low. The change only affects the design-time tools.
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.