A Question about DbContextPooling and RowLevelSecurity
At the moment I am using a DBConnectionInterceptor So I can override the ConnectionOpenedAsync function to set the Session variables I need for my RLS implementation on SQLServer.
For performance reasons I would like to switch to using DBContextPooling, but I am not sure if the above method will keep working as in my understanding the database connection could be reused. (And as that call could happen under the context of a different user, the RLS would be in an invalid state (variables pointing to another user))
Is there a hook I could use to detect that a context is "Pooled" and that an initial request to the database is made?
DbContext pooling works at the EF context level; regardless of context pooling, context use an underlying ADO.NET database connection (DbConnection) which gets pooled by the driver. So your database connections are already being reused, regardless of whether your context instances are or not.
In other words, just like you're setting the session variable when creating a new DbContext instance (without pooling), you can set the session variable when obtaining a new pooled context instance. See the docs section on managing state in pooled contexts to see how to get the information needed into your pooled context instance, which your interceptor can then apply as usual.
Thank you @roji So I am guessing that the database connection is also retrieved from a scoped service. In that case I am fine (I am using EF in a Blazor Serverside where every "user" has a different scope)
So I am guessing that the database connection is also retrieved from a scoped service.
No; database connections and their pools are managed inside the ADO.NET driver (e.g. SqlClient), and these are generally completely unaware of dependency injection. The pool is generally keyed by the connection string, so any time you use a given connection string, you're accessing the same connection pool (one exception here is when using Npgsql with the recent NpgsqlDataSource which represents a pool, and can be registered in DI like any other service).