📁 Sangam Dhakal - Portfolio v2.0 - Netscape Navigator 4.0
_
□
×

📚 Interview question

How do you combine an EF Core tenant filter with SQL Server row-level security, and why would you want both?

Mid-Level Theory Intermediate

🎯 Key points

  • EF filter: automatic tenant scoping and simpler handlers
  • RLS security policy with an inline predicate function (filter + block predicates)
  • SESSION_CONTEXT set in a DbConnectionInterceptor on every open
  • @read_only = 1 so application code can't change it mid-connection
  • Defence in depth: protects raw SQL and IgnoreQueryFilters paths
  • Index with TenantId leading to keep the predicate cheap at volume
  • Pooling: context must be set per open; test cross-tenant reads and writes

⏱ 30-second answer

The EF filter keeps normal code tenant-scoped automatically, so handlers stay simple and nobody forgets a Where. Row-level security enforces the same rule in the database: create a security policy with an inline predicate function comparing TenantId to SESSION_CONTEXT(N'TenantId') (plus a block predicate to stop cross-tenant writes), and set the session context from a DbConnectionInterceptor when each connection opens, with @read_only = 1. Together they're defence in depth: raw SQL, IgnoreQueryFilters or a bug still can't read another tenant's rows. Costs: the predicate runs on every query, so TenantId must lead the relevant indexes or RLS can turn seeks into scans on large tables; debugging is harder because rows silently disappear; and the session context must be set on every connection open because of pooling.

🔍 What the interviewer is checking

Checks defence in depth for tenant isolation.

↩ Usually asked after

More in Advanced EF Core

Last updated 23 Nov 2026.

🌐 Sangam's Webring

⏪ Previous 🏠 Main Next ⏩
💬 Chat with me