DbContextOptionsBuilder.EnableSensitiveDataLogging Doesn't Do Anything

I had the same problem. Solved it by using the OnConfiguring method in the DbContext itself, rather than the ConfigureServices method in Startup.

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder.EnableSensitiveDataLogging();
}

This option is documented here.


I don't think that's the way to enable it. This should work

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<TodoContext>(opt =>
    {
        opt.EnableSensitiveDataLogging();
    });
}

Got this exception:

System.InvalidOperationException: The instance of entity type '' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.

After enabling options.EnableSensitiveDataLogging(); like this in .NET 5

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<ApplicationDbContext>(options =>
    {
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
        options.EnableSensitiveDataLogging();
    });

The exception turned into this:

System.InvalidOperationException: The instance of entity type '' cannot be tracked because another instance with the key value '{Id: 5}' is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached.

The error turned out to be a List<MyEntity>() that contained a reference back to the value I was updating.