'Breakpoint not hit in EF Core IEntityTypeConfiguration class on add-migration in PackageManagerConsole

I am doing some discovery in a small test project and am trying to have a breakpoint hit in the configuration definition class.

public class TripExpenseComparisonEntityTypeConfiguration : IEntityTypeConfiguration<TripExpenseComparison>
{
    public void Configure(EntityTypeBuilder<TripExpenseComparison> builder)
    {
        builder.HasKey(u => u.Id)
             .HasName("PK_TravelExpense");

        builder.OwnsOne(vo => vo.BudgetedTripExpense, exp =>
        {
            var x = from p in exp.GetType().GetProperties() where p.PropertyType == typeof(decimal) select p;

            exp.Property(u => u.AllowNegatives)
                .IsRequired();

            exp.Ignore(u => u.TotalExpenses);

        });
       ... rest removed

When I run Add-Migration from the package manager console, in the DataAccess project, it runs the entire migration (correctly) EXCEPT it doesn't hit the breakpoint set on this line

>exp.Property(u => u.AllowNegatives)
        .IsRequired();

Do breakpoints not function in this type of class? Or do they not get hit during an add-migration event?



Solution 1:[1]

You can add this line Debugger.Launch(); before the required place. It launches and attaches a debugger to the process.

See the documentation here.

Another option you run the migration using DbMigrator through code and debug.

Solution 2:[2]

If you have the Database.Migrate(); call in your Startup.cs then the breakpoint should be getting hit.

If you do and it still isn't, make sure you are applying the configurations in your DbContext class.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
   modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetAssembly(typeof(MyDbContext)));
}

Where MyDbContext is the name of this class or a class in the same assembly as your IEntityTypeConfiguration<T> configurations.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 vivek nuna
Solution 2 josner