I have a readonly database connection which I turn off Tracking.
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
}
When I change my code from
builder.Services.AddDbContext<MyContext>(options => options.UseOracle(builder.Configuration.GetConnectionString("ReadOnlyConnection")));
to
builder.AddOracleDatabaseDbContext<MyContext>("ReadOnlyConnection");
When I run my code and hit the database I get:
System.InvalidOperationException: 'OnConfiguring' cannot be used to modify DbContextOptions when DbContext pooling is enabled.
If I remove the override OnConfiguring it works. Is it possible to set UseQueryTrackingBehavior else where?
AddOracleDatabaseDbContext
registers context pool via AddDbContextPool
call, which by design:
you need to pass all the context options to your
DbContextFactory
, rather than configuring them inOnConfiguring
One of the options would be to pass the configureDbContextOptions
action:
builder.AddOracleDatabaseDbContext<MyContext>("ReadOnlyConnection",
configureDbContextOptions: o => o.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking));
And remove/"fix" the OnConfiguring
override:
public class MyContext : DbContext
{
public MyContext(DbContextOptions<MyContext> options) : base(options) { }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
}
}
}
Another option would be to try registering the context "manually" and follow it with the EnrichOracleDatabaseDbContext
call as suggested in the docs.