Finbuckle / Finbuckle.MultiTenant

Finbuckle.MultiTenant is an open-source multitenancy middleware library for .NET. It enables tenant resolution, per-tenant app behavior, and per-tenant data isolation.
https://www.finbuckle.com/multitenant
Apache License 2.0
1.26k stars 261 forks source link

Sharing entities between multiple tenants #641

Open ScarlettCode opened 1 year ago

ScarlettCode commented 1 year ago

Hi,

I'm looking for some advice / best practices when it comes to sharing an entity between multiple tenants but not all tenants.

Let's say for example you have a TENANT and an EVENT. The TENANT and EVENT are linked by a TENANT_EVENT relationship as an EVENT is 1 to many with TENANTS. I can't put the TENANT_ID on the EVENT.

Is there a native way to handle this use case or should I add an additional HasQueryFilter during OnModelCreating in order to add this extra layer of filtering and get the TenantInfo to do the filtering in OnModelCreating?

AndrewTriesToCode commented 1 year ago

Hi, no native support for this scenario but I recommend you look at MultiTenantDbContext, particularly the 'OnModelBuilding' method and modify as you need it. If you aren't relying on shadow properties the lamba expression buildup can be much easier and less manual. If you just use an extra HasQueryFilter prior to Finbuckle.MultiTenant's filter being added, I believe it will do AND logic which I don't think is what you want.

Also depending on the specifics if EVENT is not configured as multitenant you should be able to query it without any issue--it won't have the tenant filter applied.

Another perhaps easier method would be to use IgnoreQueryFilters in your LINQ to bypass any tenant filtering going on and then apply your own conditions.