Search code examples
entity-framework-coreasp.net-core-mvcmigration.net-7.0

After update to .NET 7, I get an error : System.ArgumentException: An item with the same key has already been added


I created a project with ASP.NET Core 6 and developed it over time. Now I updated this project to .NET 7.

So far there have been no problems. I had no errors at runtime. The program works correctly.

After the update, when I run the add--migration command, I encounter an error whose origin is unknown.

I updated all nuget packages of the program.

Add-Migration -name test2 Build started... Build succeeded.

   at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
   at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey](IEnumerable`1 source, Func`2 keySelector, IEqualityComparer`1 comparer)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.AddSeedData(IEntityType entityType, Dictionary`2 identityMaps, EntityState initialState)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.TrackData(IRelationalModel source, IRelationalModel target, DiffContext diffContext)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetDataOperations(IRelationalModel source, IRelationalModel target, DiffContext diffContext)+MoveNext()
   at System.Linq.Enumerable.ConcatIterator`1.MoveNext()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Sort(IEnumerable`1 operations, DiffContext diffContext)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetDifferences(IRelationalModel source, IRelationalModel target)
   at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace, String language)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
An item with the same key has already been added. Key: Description

I used nlog to debug and I got this error.

Microsoft.Extensions.Hosting.HostAbortedException: The host was aborted.     at Microsoft.Extensions.Hosting.HostFactoryResolver.HostingListener.ThrowHostAborted()     at Microsoft.Extensions.Hosting.HostFactoryResolver.HostingListener.OnNext(KeyValuePair`2 value)     at System.Diagnostics.DiagnosticListener.Write(String name, Object value)     at Microsoft.Extensions.Hosting.HostBuilder.ResolveHost(IServiceProvider serviceProvider, DiagnosticListener diagnosticListener)     at Microsoft.Extensions.Hosting.HostApplicationBuilder.Build()     at Microsoft.AspNetCore.Builder.WebApplicationBuilder.Build()     at Program.<Main>$(String[] args) in H:\Project\Rafa\erp\WebUI\Program.cs:line 61

I changed nullable values with ? And I also put nullables context in the disable mode in the build propetries I also used Debugger.Launch, but i got an error on var app = builder.Build(); I encountered an exception

Microsoft.Extensions.Hosting.HostAbortedException
  HResult=0x80131500
  Message=The host was aborted.
  Source=Microsoft.EntityFrameworkCore.Design
  StackTrace:
   at Microsoft.Extensions.Hosting.HostFactoryResolver.HostingListener.ThrowHostAborted()
   at Microsoft.Extensions.Hosting.HostFactoryResolver.HostingListener.OnNext(KeyValuePair`2 value)
   at System.Diagnostics.DiagnosticListener.Write(String name, Object value)
   at Microsoft.Extensions.Hosting.HostBuilder.ResolveHost(IServiceProvider serviceProvider, DiagnosticListener diagnosticListener)
   at Microsoft.Extensions.Hosting.HostApplicationBuilder.Build()
   at Microsoft.AspNetCore.Builder.WebApplicationBuilder.Build()
   at Program.<Main>$(String[] args) in H:\Project\Rafa\erp\WebUI\Program.cs:line 61

This exception was originally thrown at this call stack:

    [External Code]
    Program.<Main>$(string[]) in Program.cs

Solution

  • In my case, I had a HasData statement for Entity type A but I was creating a different type B. I'm surprised this even compiled. It took me a while to find.

    builder.Entity<A>().HasData(new B { ... });