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.3k stars 265 forks source link

The column "TenantId" does not exist #300

Closed win32nipuh closed 3 years ago

win32nipuh commented 4 years ago

I use latest FB preview, EF 3.1.4 for PostgreSQL. I am not sure what is the source of the problem but i receive the exception during query my Customers Set: Column does not exist: i.e. I see the query

   {SELECT c.id, c.accounts_contact, c.address,c.name, c.tel, c."TenantId", c.towncity, c.web
FROM customers AS c
WHERE c."TenantId" = $1}

The matter is: the field "TenantId" was added (why) to the correct query, but it is not in the table and naturally PostgreSQL error. Why the field "TenantId" is in the query? How I can fix it?

AndrewTriesToCode commented 4 years ago

hi @win32nipuh Can you post your dbcontext class as well as your customer entity class?

The "where" clause is coming from the global query filter Finbuckle.MultiTenant adds as a part of its core functionality--its how it isolates tenant data. If you don't want this behavior I can help you modify your context and entity class to not treat customers per-tenant.

ZedZipDev commented 4 years ago

Hey Andrew, I see. Ok, I will send the files. As I understand it is necessary to have that field in each table in case of using one database for all tenants. But I'd like to have separate databases. Btw, probably you can add some configuration parameter: to add the Tenantid field or do not.

AndrewTriesToCode commented 4 years ago

@win32nipuh

Based on your email I would recommend that your db context NOT derive from MultiTenantDbContext. It's main functionality is to support tenants sharing a database, but your use case is separate database per tenant.

In your case I recommend you inherit from the normal DbContext', inject the tenant info into the constructor, and set the connection inOnConfiguringusing the tenant infoConnectionString`.

win32nipuh commented 4 years ago

Ok, by the way:

The DataIsolationSample uses 3 separate databases for 3 tenants. In fact: the field TenantId is not needed in the tables. IMHO, this sample can be divided to 2 or some configuration parameter can be used etc, you know better:

  1. one common database for each tenant, then TenantId is in each table as now, ok
  2. separate databases for every tenant, then we do not need the TenantId. it does not make sense
AndrewTriesToCode commented 4 years ago

Hi, In the sample two of them should be sharing a single database and one should have it’s own. I’ll double check it today and maybe I can make it more clear. I think it might be clearer to have separate sample projects like you say but also to have the existing one-there are cases where you might have a mix of shared and separate databases.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.