Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Core Add Migration Debugging

How can I step into OnModelCreating with a breakpoint and see if my logic is wrong or if the ModelBuilder is doing something I'm not expecting? I've seen lots of posts on how to debug the actual migration, but nothing on how to watch how the model code is being generated.

I'm trying to implement some custom attributes on some of my entities, and it's being ignored; I'd like to see what my configuration is doing as it's generating the model code.

like image 569
Jeremy Holovacs Avatar asked Jan 15 '17 16:01

Jeremy Holovacs


People also ask

How do I add-migration to EF core?

Adding a Migration So, firstly, you need to create a migration. Open the Package Manager Console from the menu Tools -> NuGet Package Manager -> Package Manager Console in Visual Studio and execute the following command to add a migration.

What does add-migration do?

Add-Migration: Creates a new migration class as per specified name with the Up() and Down() methods. Update-Database: Executes the last migration file created by the Add-Migration command and applies changes to the database schema.

Does EF core support automatic migration?

EF core doesn't support automatic migrations . So you have to do it manually. From the perspective of automatic migrations as a feature, we are not planning to implement it in EF Core as experience has showed code-base migrations to be a more manageable approach.

What is PMC migration command in Entity Framework Core?

The Package Manager Console (PMC) tools for Entity Framework Core perform design-time development tasks. For example, they create migrations, apply migrations, and generate code for a model based on an existing database. The commands run inside of Visual Studio using the Package Manager Console.


2 Answers

You should be able to call Debugger.Launch() in your code. The just-in-time debugger should prompt you to attach a debugger when it hits that line.

like image 132
bricelam Avatar answered Sep 18 '22 12:09

bricelam


An alternate method would be to create your own console app or unit test and debug that. Use this snippet by bricelam from the ef github issue

using (var db = new MyDbContext()) {     var reporter = new OperationReporter(         new OperationReportHandler(             m => Console.WriteLine("  error: " + m),             m => Console.WriteLine("   warn: " + m),             m => Console.WriteLine("   info: " + m),             m => Console.WriteLine("verbose: " + m)));      var designTimeServices = new ServiceCollection()         .AddSingleton(db.GetService<IHistoryRepository>())         .AddSingleton(db.GetService<IMigrationsIdGenerator>())         .AddSingleton(db.GetService<IMigrationsModelDiffer>())         .AddSingleton(db.GetService<IMigrationsAssembly>())         .AddSingleton(db.Model)         .AddSingleton(db.GetService<ICurrentDbContext>())         .AddSingleton(db.GetService<IDatabaseProvider>())         .AddSingleton<MigrationsCodeGeneratorDependencies>()         .AddSingleton<ICSharpHelper, CSharpHelper>()         .AddSingleton<CSharpMigrationOperationGeneratorDependencies>()         .AddSingleton<ICSharpMigrationOperationGenerator, CSharpMigrationOperationGenerator>()         .AddSingleton<CSharpSnapshotGeneratorDependencies>()         .AddSingleton<ICSharpSnapshotGenerator, CSharpSnapshotGenerator>()         .AddSingleton<CSharpMigrationsGeneratorDependencies>()         .AddSingleton<IMigrationsCodeGenerator, CSharpMigrationsGenerator>()         .AddSingleton<IOperationReporter>(reporter)         .AddSingleton<MigrationsScaffolderDependencies>()         .AddSingleton<MigrationsScaffolder>()         .BuildServiceProvider();      var scaffolder = designTimeServices.GetRequiredService<MigrationsScaffolder>();      var migration = scaffolder.ScaffoldMigration(         "MyMigration",         "MyApp.Data");      File.WriteAllText(         migration.MigrationId + migration.FileExtension,         migration.MigrationCode);     File.WriteAllText(         migration.MigrationId + ".Designer" + migration.FileExtension,         migration.MetadataCode);     File.WriteAllText(migration.SnapshotName + migration.FileExtension,         migration.SnapshotCode); } 
like image 25
Tedy Pranolo Avatar answered Sep 17 '22 12:09

Tedy Pranolo