DbContextOptionsBuilder.EnableSensitiveDataLogging Doesn't Do Anything

Sim*_*gan 5 c# asp.net entity-framework

I'm trying to track down the cause of an Entity Framework InvalidOperationException in an ASP.NET Core project. The exception suggests using DbContextOptionsBuilder.EnableSensitiveDataLogging.

In my Startup.cs I have:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<TodoContext>(opt =>
    {
        opt.UseInMemoryDatabase("TodoList");
        opt.EnableSensitiveDataLogging();
    });
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
Run Code Online (Sandbox Code Playgroud)

The problem is it doesn't seem to do anything. The exception message I get is exactly the same and still suggests using DbContextOptionsBuilder.EnableSensitiveDataLogging.

Am I missing something?

Ogg*_*las 10

得到这个异常:

System.InvalidOperationException:无法跟踪实体类型“”的实例,因为已跟踪具有相同键值 {'Id'} 的另一个实例。附加现有实体时,请确保仅附加一个具有给定键值的实体实例。考虑使用“DbContextOptionsBuilder.EnableSensitiveDataLogging”来查看冲突的键值。

options.EnableSensitiveDataLogging();在.NET 5中启用后

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

异常变成了这样:

System.InvalidOperationException:无法跟踪实体类型“”的实例,因为已跟踪具有键值“{Id:5}”的另一个实例。附加现有实体时,请确保仅附加一个具有给定键值的实体实例。

结果发现错误List<MyEntity>()包含对我正在更新的值的引用。


Fra*_*edu 8

我不认为这是启用它的方法。这应该有效

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<TodoContext>(opt =>
    {
        opt.EnableSensitiveDataLogging();
    });
}
Run Code Online (Sandbox Code Playgroud)


chr*_*ian 7

我有同样的问题。通过使用本身的OnConfiguring方法解决它DbContext,而不是使用中的ConfigureServices方法Startup

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder.EnableSensitiveDataLogging();
}
Run Code Online (Sandbox Code Playgroud)

此选项记录在此处