46

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.

Vahid Farahmandian
  • 6,081
  • 7
  • 42
  • 62
Jeremy Holovacs
  • 22,480
  • 33
  • 117
  • 254
  • Possible duplicate of [Can one set a breakpoint in EF code first migrations seed method?](https://stackoverflow.com/questions/28858289/can-one-set-a-breakpoint-in-ef-code-first-migrations-seed-method) – Michael Freidgeim Feb 19 '18 at 11:00
  • They may have similar answers, but they are entirely different questions. – Jeremy Holovacs Feb 21 '18 at 13:09

2 Answers2

101

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.

bricelam
  • 28,825
  • 9
  • 92
  • 117
2

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);
}
Tedy Pranolo
  • 1,285
  • 1
  • 14
  • 22