title: Debugging Code Called by EF Core Add-Migrations
body: <p>I have an Entity Framework Core database defined in a separate assembly, using the IDesignTimeDbContextFactory&lt;> pattern (i.e., I define a class, derived from IDesignTimeDbContextFactory, which has a method called CreateDbContext that returns an instance of the database context).</p>
  
  <p>Because the application of which the EF Core database is a part utilizes AutoFac dependency injection, the IDesignTimeDbContextFactory&lt;> factory class creates an AutoFac container in its constructor, and then resolves the DbContextOptionsBuilder&lt;>-derived class which is fed into the constructor for the database context (I do this so I can control whether a local or an Azure-based SqlServer database is targeted, based on a config file setting, with passwords stored in an Azure KeyVault):</p>
  
  <pre><code>public class TemporaryDbContextFactory : IDesignTimeDbContextFactory&lt;FitchTrustContext&gt;
  {
      private readonly FitchTrustDBOptions _dbOptions;
  
      public TemporaryDbContextFactory()
      {
          // OMG, I would &gt;&gt;never&lt;&lt; have thought to do this to eliminate the default logging by this
          // deeply-buried package. Thanx to Bruce Chen via 
          // https://stackoverflow.com/questions/47982194/suppressing-console-logging-by-azure-keyvault/48016958#48016958
          LoggerCallbackHandler.UseDefaultLogging = false;
  
          var builder = new ContainerBuilder();
  
          builder.RegisterModule&lt;SerilogModule&gt;();
          builder.RegisterModule&lt;KeyVaultModule&gt;();
          builder.RegisterModule&lt;ConfigurationModule&gt;();
          builder.RegisterModule&lt;FitchTrustDbModule&gt;();
  
          var container = builder.Build();
  
          _dbOptions = container.Resolve&lt;FitchTrustDBOptions&gt;() ??
                       throw new NullReferenceException(
                           $"Could not resolve {typeof(FitchTrustDBOptions).Name}");
      }
  
      public FitchTrustContext CreateDbContext( string[] args )
      {
          return new FitchTrustContext( _dbOptions );
      }
  }
  
  public class FitchTrustDBOptions : DbContextOptionsBuilder&lt;FitchTrustContext&gt;
  {
      public FitchTrustDBOptions(IFitchTrustNGConfigurationFactory configFactory, IKeyVaultManager kvMgr)
      {
          if (configFactory == null)
              throw new NullReferenceException(nameof(configFactory));
  
          if (kvMgr == null)
              throw new NullReferenceException(nameof(kvMgr));
  
          var scannerConfig = configFactory.GetFromDisk()
                              ?? throw new NullReferenceException(
                                  "Could not retrieve ScannerConfiguration from disk");
  
          var dbConnection = scannerConfig.Database.Connections
                                 .SingleOrDefault(c =&gt;
                                     c.Location.Equals(scannerConfig.Database.Location,
                                         StringComparison.OrdinalIgnoreCase))
                             ?? throw new ArgumentOutOfRangeException(
                                 $"Cannot find database connection information for location '{scannerConfig.Database.Location}'");
  
          var temp = kvMgr.GetSecret($"DatabaseCredentials--{dbConnection.Location}--Password");
  
          var connString = String.IsNullOrEmpty(dbConnection.UserID) || String.IsNullOrEmpty(temp)
              ? dbConnection.ConnectionString
              : $"{dbConnection.ConnectionString}; User ID={dbConnection.UserID}; Password={temp}";
  
          this.UseSqlServer(connString,
              optionsBuilder =&gt;
                  optionsBuilder.MigrationsAssembly(typeof(FitchTrustContext).GetTypeInfo().Assembly.GetName()
                      .Name));
      }
  }
  </code></pre>
  
  <p>Needless to say, while this provides me with a lot of flexibility (I can switch from local to cloud database just by changing a single config parameter, and any required passwords are reasonably securely stored in the cloud), it can trip up the add-migration commandlet if there's a bug in the code (e.g., the wrong name of a configuration file).</p>
  
  <p>To debug those kinds of problems, I've often had to resort to outputting messages to the Visual Studio output window via diagnostic WriteLine calls. That strikes me as pretty primitive (not to mention time-consuming).</p>
  
  <p>Is there a way to attach a debugger to my code that's called by add-migration so I can step thru it, check values, etc? I tried inserting a Launch() debugger line in my code, but it doesn't work. It seems to throw me into add-manager codebase, for which I have no symbols loaded, and breakpoints that I try to set in my code show up as the empty red circle: they'll never be hit.</p>
  
  <p>Thoughts and suggestions would be most welcome!</p>