question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there anyway to debug Add-Migrations?

See original GitHub issue

I refactor my code to inherit my entities from an abstract generic type. If I Add-Migrations from Existing migrations it give me error.
But, If I delete all previous Migrations and run Add-Migrations. It will create a new successful migration file. The error is the following:

System.ArgumentException: An item with the same key has already been added. Key: System.Object Item [System.String]
   at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetSortedProperties(IEntityType entityType, ITable table)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetSortedColumns(ITable table)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Add(ITable target, DiffContext diffContext)+MoveNext()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.DiffCollection[T](IEnumerable`1 sources, IEnumerable`1 targets, DiffContext diffContext, Func`4 diff, Func`3 add, Func`3 remove, Func`4[] predicates)+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)

Is there anyway to debug the Add-Migrations process? because the error message with “Key: System.Object Item [System.String]” is not helpful. I cannot know which part of my code might have conflicted key.

EF Core version: Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer) Target framework: (e.g. .NET 6.0) Operating system: IDE: (e.g. Visual Studio 2022 17.3.4)

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
qsdfplkjcommented, Sep 16, 2022

I add if (!Debugger.IsAttached) Debugger.Launch(); in my IDesignTimeDbContextFactory so I only have to attach a debugger when working with the migrations. But any other point that runs before it’s configuring the model (like ConfigureServices or in OnConfiguring as the first line or just the constructor of the dbContext will enable you to attach the debugger.

0reactions
rojicommented, Sep 17, 2022

Here’s a snippet I sometimes use to add a migration from a console program, in order to debug issues:

using var ctx = new BlogContext();

var services = new ServiceCollection()
    .AddEntityFrameworkDesignTimeServices()
    .AddDbContextDesignTimeServices(ctx);

var npgsqlDesignTimeServices = new NpgsqlDesignTimeServices();
npgsqlDesignTimeServices.ConfigureDesignTimeServices(services);

var serviceProvider = services.BuildServiceProvider();
var scaffolder = serviceProvider.GetRequiredService<IMigrationsScaffolder>();
var migration = scaffolder.ScaffoldMigration(
    "MyMigration",
    "MyApp.Data");

Console.WriteLine(migration.MigrationCode);
Read more comments on GitHub >

github_iconTop Results From Across the Web

EF Core Add Migration Debugging
I've seen lots of posts on how to debug the actual migration, but nothing on how to watch how the model code is...
Read more >
Debugging Entity Framework Core migration scaffolding and ...
Debugging migration application. Let's start with the easier part - debugging the situation when errors appear while changes to the database ...
Read more >
Debugging your Entity Framework Migration Seeding
Leave the debugger (Second instance of Visual Studio) open between runs. You can start and stop the first instance at will. Your breakpoints ......
Read more >
Debugging migrations
This page describes how migration definitions can be debugged.
Read more >
Debugging Your AWS DMS Migrations: What to Do When ...
This post gives you a quick introduction to the initial steps that you need to take to debug AWS DMS migrations. It also...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found