-
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
Add-Migration fails with "An item with the same key has already been added" when renaming entity #27504
Comments
Workaround
|
@bricelam I think we might have seen this before. |
Looks like #26405, but that was supposed to be fixed in 6.0.2. |
@srmagura I think we're going to need a full repro for this. |
@ajcvickers No problem. Are you all right with me sending the full project (with repro instructions), or should I work on trimming out everything that is not relevant to the bug? |
@srmagura The latter is always preferred. Assuming there is a bug here, then that work will have to be done by someone, and it's often easier for the person who knows the code than it is for someone like me who has to figure out how everything in the project is organized and fits together. |
Sounds good, I will work on it. It may be about 2 weeks — I'll comment here when it is ready. |
Minimal repro is done! https://github.com/srmagura/EFIssue27504 The README includes instructions for reproducing the bug. Thank you. |
Is it fixed on 6.0.2? I'm still getting it on 6.0.4
|
@mjamro Make sure to update all EF packages, not just the tools |
Hello,
There is workaround:
|
I just got this error after doing major code refactoring. This error appear, if I remove all previous migrations & DbContextModelSnapshot, then recreate one it will be successful. Is there anyway to debug migrations tools? I need to find out where is the problem. |
Got the same when adding a migration that removes two completely independent DbSets (flat tables no relationships etc). |
I got the same error, I'm using entities with owned entities. |
Seeing the same in 6.0.1 caused by removing an entity (and the associated DbSet on the DbContext) that had an owned entity. I was able to work around this by removing the representation of said entity in the snapshot, scaffolding an empty migration and populating the respective Up and Down from the initial migration that was already present.
|
+1.. var migrations = db.GetService<IMigrationsAssembly>();
var init = db.GetService<IModelRuntimeInitializer>();
var model = migrations.ModelSnapshot.Model;
if (model is IMutableModel m)
model = m.FinalizeModel();
model = init.Initialize(model);
var relational = model.GetRelationalModel();
var diffOperations = modelDiffer.GetDifferences(null, relational); However at
linkingNavigationProperty is always the same PropertyInfo ; Dictionary<string,object>.Item[string] . Leading to the same duplicate key exception as seen above.
Perhaps this dummy |
Adding an explicit test for foreach (var linkingForeignKey in table.GetReferencingRowInternalForeignKeys(entityType))
{
var linkingNavigationProperty = linkingForeignKey.PrincipalToDependent?.PropertyInfo;
var properties = GetSortedProperties(linkingForeignKey.DeclaringEntityType, table).ToList();
if (linkingNavigationProperty == null
+ || linkingNavigationProperty.DeclaringType == typeof(Dictionary<string,object>))
{
leastPriorityProperties.AddRange(properties);
continue;
} Or should that be something like; foreach (var linkingForeignKey in table.GetReferencingRowInternalForeignKeys(entityType))
{
var properties = GetSortedProperties(linkingForeignKey.DeclaringEntityType, table).ToList();
var linkingNavigation = linkingForeignKey.PrincipalToDependent;
if (linkingNavigation?.PropertyInfo == null
|| linkingNavigation.IsIndexerProperty()
|| linkingNavigation.IsShadowProperty())
{
leastPriorityProperties.AddRange(properties);
continue;
}
var linkingNavigationProperty = linkingNavigation.PropertyInfo;
groups.Add(linkingNavigationProperty, properties); |
Hi there! Anything happening on this bug? I just did a big refactor on my project, both dropping (empty) and adding tables, incl. owned-properties. I'm using .net6 and tried EF Core Tools 6.0.12 and now even 7.0.1 (still using .net6).
This should probably rank as one of the most useless error messages I have seen, so I really don't know what to do. Considering recreating the entire database right now as I was just suppose to clean up and publish this... Best regards, |
This just hit us on a large project that has recently been upgraded to .net7 and EF Core 7.01. We are refactoring to remove all OwnedEntities. We removed one, successfully added a migration, then spent hours factoring out dozens of other owned entites. Then we tried to add another migration and this error hit. The error is so vague it gives no clue how to proceed.
|
This bug is triggered while sorting columns for a create table operation. When two classes map to the same table, via a navigation property that is also an "indexer property". When loading a model snapshot, all properties are indexer properties. So this bug is most likely triggered when the model differ can't locate a matching table in your new model, and needs to generate a create table operation for the Down script. You could try scaffolding your schema in a blank project. Adding a migration. Then moving the contents of the generated model snapshot to your code first project. Avoiding any shared tables in the model snapshot. |
The one line diff I posted above should avoid the exception. But a better fix would sort columns without using For my own project I replaced the model differ service to test the one line code change. However, overriding the implementation of |
It happened to me too after upgrade following packages:
Now I cannot create any Migration because of this error, so I had to downgrade these packages. |
My error, and how I resolved itI encountered the same error message when using CodeDbContext snippet
Foo snippet
Bar snippet
The problemRemoving the How I resolved it
ConclusionI am relatively new to Entity Framework Core, so I cannot diagnose what initially caused this error, but I assume it has something to do with deleting entities that have owned entities attached. |
@AndriySvyryd I just tested the latest dotnet ef cli, that I think should contain the fixe #29981, and I still get the error. PS C:\Users\apaquette> dotnet tool uninstall -g dotnet-ef
L'outil 'dotnet-ef' (version '7.0.2') a été désinstallé.
PS C:\Users\apaquette> dotnet tool install -g dotnet-ef --version 8.0.0-* --add-source https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet8/nuget/v3/index.json
Vous pouvez appeler l'outil à l'aide de la commande suivante : dotnet-ef
L'outil 'dotnet-ef' (version '8.0.0-alpha.1.23066.4') a été installé correctement. I have a table that contains owned types and this table is not registered with a Was it supposed to work or is it the same message for an other error? |
@adampaquette This fix is not included in 7.0.2. Check the milestone on the issue. |
@ajcvickers This is why I installed |
Ah sorry, missed that. The version of the CLI tool doesn't usually matter. Make sure your project is using the latest daily build packages. |
@ajcvickers We're also running into this issue when changing (and renaming) a 1:n entity to an owned entity. The workaround of ignoring unfortunately does not work. It appears that there are no pre-releases of EF 7.0.3 on the NuGet feed, is that observation correct? edit: Unfortunately EF 8.0.0 alpha has the same issue. Happy to send the solution. |
@Sebazzz Please open a new issue and attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate. |
Needed to purge migrations and database due to bug: dotnet/efcore#27504
Needed to purge migrations and database due to bug: dotnet/efcore#27504
I made this change to my
DbContext
and nowAdd-Migration
fails:In words: I renamed the
DbImport
entity class AND renamed theImports
table.Include your code
I have tried to create a minimal reproduction of the issue but can't get the error to occur. If necessary, I can strip out everything from my real codebase until I have a small project that demonstrates the bug (but this would be a lot of work).
Both me and my coworker are able to consistently reproduce the bug on our real closed-source codebase. I am happy to send this codebase to any EF Core contributor as a private GitHub repo.
Include verbose output
Output from
Add-Migration
in Package Manager Console in Visual Studio 2022:Include provider and version information
EF Core version: 6.0.2
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0
Operating system: Windows 11
IDE: Visual Studio 2022 17.1.0
The text was updated successfully, but these errors were encountered: