UPD here is the way I solved the problem. Although it\'s likely to be not the best one, it worked for me.
I have an issue with working with EF
I actually found it to be a simpler solution with an EF interceptor.
I actually keep the onModeling method:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("dbo"); // this is important to always be dbo
// ... model definition ...
}
And this code will be in Startup:
public void ConfigureServices(IServiceCollection services)
{
// if I add a service I can have the lambda (factory method) to read from request the schema (I put it in a cookie)
services.AddScoped(provider =>
{
var context = provider.GetService().HttpContext;
var scheme = "dbo";
if (context.Request.Cookies["schema"] != null)
{
scheme = context.Request.Cookies["schema"];
}
return new SchemeInterceptor(scheme);
});
services.AddDbContext(options =>
{
var sp = services.BuildServiceProvider();
var interceptor = sp.GetService();
options.UseSqlServer(Configuration.GetConnectionString("Default"))
.AddInterceptors(interceptor);
});
And the interceptor code looks something like this (but basically we use ReplaceSchema):
public interface ISchemeInterceptor : IDbCommandInterceptor
{
}
public class SchemeInterceptor : DbCommandInterceptor, ISchemeInterceptor
{
private readonly string _schema;
public SchemeInterceptor(string schema)
{
_schema = schema;
}
public override Task> ScalarExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult