Basic Multitenant Application With ASP.NET Core

A step-by-step guide on how to implement a Multitatent architecture using .NET Core.

Pritomsarkar
.Net Programming
Published in
4 min readApr 30, 2021

--

Overview

A multi-tenant application is adept at serving multiple tenants the same code base. Multi-Tenant Architecture generally means a single site that services two or more domains. so here a domain is an address where Internet users can access the website.

Prerequisites

I expect you to have knowledge of object-oriented programming concepts in C#. I assume you know .NET Core concepts especially the MVC pattern also.

To code along with me, you will have to install the .NET Core 2.2, as well as Visual Studio. You can also use another IDE instead of a visual studio.You can find a link to the Github repository end of this article.

Environment Setup

Step-1:-

Open Visual Studio 2017. Create a project “Asp.Net Core Web Application(.Ne Core)” with an MVC pattern Template. You Can name this project whatever you want. Let’s fun begin!

Configure Asp.Net Core

Step-2:- Now we’ll install the dependencies if it’s not installed already in your IDE.

Microsoft.AspNetCore.Razor.Design

Microsoft.VisualStudio.Web.CodeGeneration.Design

Microsoft.AspNetCore.App

Step-3:-

Now Create many class files under the Models folder. (Models/…)

Models/Contact.cs
Models/Customer.cs

Step-4:-

Now create a DB-Context model inside of the data folder.

data/ApplicationDbContext.cs

Before migrating and update data, you have to create a database for your application.

So you have seen, in the above code, there used two connection strings. so you can think why actually used two connection- strings in the above code? Here “DefaultConnection” used for the main purpose. and “TemplateConnection” used for connection string input purposes. you can input a new connection string whatever you want for “TemplateConnection”.make sure your input’s keyword of connection string will stay on your database server. Now you have to something change in your startup class to perfectly working in your connection string.

services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));

And then you can migrate and update your database. but the important point to keep in mind is that You have to create two separate data tables in your database server same as my main Database’s process. Use the EF Core migrations feature for managing data model changes in an ASP.NET Core application. don’t forget to migration of every separate DB in your application. because we are going to access these separate tables data into one application. so migration is very important here. So, separate two database’s names is:-MultiTenant_ClientA,MultiTenant_ClientB.

Step-5:-

Now time to create a custom service for the main process in our application.

Models/CustomerDataContextExtensions.cs

So what actually works in this above code? these codes actually main to process created in our multitenant architecture. it’s a custom service in our application. when I type URL into my browser:-http://localhost:5000/clienta/contacts this code,

var clientSlug = httpContext.HttpContext.Request.Path.Value.Split(“/”, StringSplitOptions.RemoveEmptyEntries)[0];

is working for collecting the database name. so the question is how?

actually, clientSlug is collect database name from URL. I made these processes. but you can make any different process whatever you want to collect your DB.you can make this direct input for collecting your DB. Now, this code,

var connString = configuration.GetConnectionString("TemplateConnection").Replace("__DBNAME__", $"MultiTenant_{clientSlug}");

already I told, TemplateConnection is especially will use for input DB using my keyword. so here TemplateConnection will work as a main. according to my above URL clientSlug value is clienta. we will found the connString value is like our appsettings.json files connection strings and DB name is Multitenant_clienta. I already created Multitenant_ClientA.that means now we can access this DB property. So now we can change dynamically DB using DbContextoptionsBuilder.actually, it’s doing here override.

Step-6:-

Now should be doing some changes in our startup class to work on your custom service. here I give you the whole startup class.

Step-7:-

So we already did the main work of the multitenant. Now we receive our dynamic DB.and now we will found its data table’s property.so I have to use a controller and view.

Step-8:-

Now time to run this application.

Conclusion

This article hasn’t covered all aspects of a professional Multitenant, but you learned some of the basics. you can download this source code in my Github Repository. actually, it’s my first time writing an article. hope you guys enjoy this article.

--

--

.Net Programming
.Net Programming

Published in .Net Programming

Explore the .Net with the latest in C# from basic to advanced, including .Net versions 9, 8, 6, 5, Core 3.1, .Net Framework, ASP.NET Core, MVC, design patterns, OOPS, and SOLID principles. Get top tutorials, best practices, and hands-on code examples on GitHub.

No responses yet