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.

5/7/2019 1:18:33 PM

Accepted Answer

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.

1/20/2017 9:43:52 PM

Popular Answer

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<ICSharpHelper, CSharpHelper>()
        .AddSingleton<ICSharpMigrationOperationGenerator, CSharpMigrationOperationGenerator>()
        .AddSingleton<ICSharpSnapshotGenerator, CSharpSnapshotGenerator>()
        .AddSingleton<IMigrationsCodeGenerator, CSharpMigrationsGenerator>()

    var scaffolder = designTimeServices.GetRequiredService<MigrationsScaffolder>();

    var migration = scaffolder.ScaffoldMigration(

        migration.MigrationId + migration.FileExtension,
        migration.MigrationId + ".Designer" + migration.FileExtension,
    File.WriteAllText(migration.SnapshotName + migration.FileExtension,

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow