-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Bug: ToView + Navigation property + HasPrincipalKey breaks migrations (with root cause identification) #21165
Comments
After further investigation:
As far as I can asses this could either be fixed by:
The second solution would likely also cover other edge cases outside of this issue that stem from the snapshot not containing readonly entities. Note:
|
Notes for implementation: efcore/src/EFCore.Design/Migrations/Design/CSharpSnapshotGenerator.cs Lines 651 to 657 in 9f316e5
|
Is there any chance to see a bugfix for this in 3.x? Totally understand if not just trying to understand if this is rather short term ro timeline is release of 5.0 If it wont be available before 5.0 release do you have any suggestions for a workaround? Here are the ones we came up with:
Can you think of any other options? We are not afraid to meddle with internal infrastructure if necessary. When a patch comes along we would then be able to remove whatever intermediate solution there might be and not have to change the business logic of our app. |
In the model snapshot file, just add modelBuilder.Entity("EntityB").HasAlternateKey("AlternateKeyOnEntityB"); That will register the alternate key and future migration will stop generating operation in Up method. |
@smitpatel You are correct that when editing the snapshot the next migration will be generated correctly. However the snapshot after the migration does not contain the alternate key anymore. Meaning developers would need to remember todo it (similar to option 1 above). Still manipulating the snapshot file is the most promising avenue. I have looked into whether these unrecognized keys could be defined in a partial class, but there is no way to hook into We are now looking at one of two options:
The last will be what we are focusing on. Will report back here if this worked out for others to find. |
We found another workaround using compiler symbols, the general idea is
We have defined a custom compiler symbol SKIPVIEWRELATIONS, to be able to quickly identify it later in our code. The only downside is that devs need to define the alternate key as well as the relation, but that's manageable. The benefits are that once setup correctly:
I wanne thank you for the time you invested to pinpoint this issue which allowed to to come up with this workaround. |
When having the following scenario configured in the model:
It results in:
ef migrations update
fail moving forwardTo be clear this happens not just the first time after adding, but every time we create a new migration from now on.
Please see the Identifying the root cause section at the end as I think I have pinpointed the problem.
Business Scenario:
Steps to reproduce
Identifying the root cause:
IsIgnoredByMigrations
extension method is calledToView
Further technical details
EF Core version: 3.1.3
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.1
Operating system: Windows
IDE: Visual Studio 2019 16.3, but doesn't matter as migrations were crated via
dotnet ef
cliThe text was updated successfully, but these errors were encountered: