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.33k stars 264 forks source link

Newbie Question - how do I provision the store? #552

Closed jimmywim closed 2 years ago

jimmywim commented 2 years ago

I'm intending on using this library to build a multi-tenant app, using WithEFCoreStore().

I'm following the sample code fine, and I have an existing tenant DB that I'm adding to the store (in app.IsDevelopment()) - but the big problem is the store's database doesn't exist!

I'm using an IOptions<T> to hold the connection string to the database, which is injected fine into my store's DB Context type, and it uses optionsBuilder.UseSqlServer() passing in the (correct) connection string here, but nothing is creating the Store's DB if it doesn't exist.

Do we need to manage the schema of that database outside of anything provided by MultiTenant? If so that's fine, and does the schema of the store's DB that we manage just follow the ITenantInfo shape in terms of properties, etc?

As a note, if I manually create an (empty) database for the store, the error I get when calling TryAddAsync() is:

---> Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid object name 'TenantInfo'.

Does that suggest we create a table called TenantInfo to store the tenant details as per the ITenantInfo shape (especially if a custom type is inherting it with more properties)?

Any other tables needed by the EFCoreStore?

AndrewTriesToCode commented 2 years ago

hi @jimmywim sorry for the late reply!

The short answer is that EFCore migrations are needed to initialize the schema on a fresh server. Nothing specific to Finbuckle here, but you'll want to check out the dotnet ef database update command.

In many of my samples I programmatically initiallze and migrate to make them run smoother. You can do this as well if you prefer using the DbContext.Database EnsureCreated and ApplyMigrations methods if I remember correctly.

Again, sorry for the late reply and I hope you find this library useful.

jimmywim commented 2 years ago

Hi @AndrewTriesToCode - thanks for the reply and clarification - I managed to get local working by hand-cranking it (which I prefer to do as I like using dacpacs in Azure DevOps pipelines for deployment). Good to know that there the migrations strategy will work if needed.

I'm quite happy with this library actually, I managed to get Tenant per Azure AD Tenancy working using the claims strategy and passing through the full Claim Type that AAD uses: http://schemas.microsoft.com/identity/claims/tenantid - (noting this here in case anyone else stumbles across it whilst searching for the same solution).

I'll mark this as closed.