Sponsor: Using RabbitMQ or Azure Service Bus in your .NET systems? Well, you could just use their SDKs and roll your own serialization, routing, outbox, retries, and telemetry. I mean, seriously, how hard could it be?

Entity
Let’s jump right into some sample code of a simple Entity that represents a customer. Notice theTenantId
.
Filter
The approach we are going to use is to pre-filter anyDbSet
in our DbContext
. We can do this by using the EntityFramework-Plus package.
It provides us the ability to specify per context instance how to pre-filter our DbSets. We can do this by adding our TenantId
as a ctor parameter and use the Filter<T>
extension method.
Example Usage
Here’s a small console app that adds two new customer records both with the sameCustomerId = 1
. When creating the MyDbContext
, we the TenantId
we want to pre-filter on.