{"id":18504193,"url":"https://github.com/viniciuscestarii/multi-tenant-dotnet-app","last_synced_at":"2025-04-22T11:27:50.577Z","repository":{"id":260665868,"uuid":"881999412","full_name":"ViniciusCestarii/Multi-Tenant-Dotnet-App","owner":"ViniciusCestarii","description":"🌆 A sample multi-tenant application using ASP.NET Core and Entity Framework Core","archived":false,"fork":false,"pushed_at":"2025-02-18T11:23:26.000Z","size":33,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-29T14:41:20.890Z","etag":null,"topics":["asp-net-core","dotnet8","entity-framework-core","multi-tenant","postgresql"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ViniciusCestarii.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-01T17:02:37.000Z","updated_at":"2025-03-08T16:24:07.000Z","dependencies_parsed_at":"2024-11-01T18:29:35.834Z","dependency_job_id":null,"html_url":"https://github.com/ViniciusCestarii/Multi-Tenant-Dotnet-App","commit_stats":null,"previous_names":["viniciuscestarii/multi-tenant-dotnet-app"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ViniciusCestarii%2FMulti-Tenant-Dotnet-App","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ViniciusCestarii%2FMulti-Tenant-Dotnet-App/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ViniciusCestarii%2FMulti-Tenant-Dotnet-App/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ViniciusCestarii%2FMulti-Tenant-Dotnet-App/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ViniciusCestarii","download_url":"https://codeload.github.com/ViniciusCestarii/Multi-Tenant-Dotnet-App/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250231007,"owners_count":21396401,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asp-net-core","dotnet8","entity-framework-core","multi-tenant","postgresql"],"created_at":"2024-11-06T14:03:27.804Z","updated_at":"2025-04-22T11:27:50.562Z","avatar_url":"https://github.com/ViniciusCestarii.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Multi-Tenant .NET App\n\nThis is a sample multi-tenant application that demonstrates how to build a multi-tenant application using ASP.NET Core and Entity Framework Core.\n\n## Features\n\n- Tenant Resolution Strategy\n\n###### CurrentTenantService.cs\n```cs\nvar tenantInfo = await _context.Tenants.Where(x =\u003e x.Id == tenant).FirstOrDefaultAsync(); // check if tenant exists\nif (tenantInfo != null)\n{\n    TenantId = tenant;\n    ConnectionString = tenantInfo.ConnectionString; // optional connection string per tenant (can be null to use default database)\n    return true;\n}\nelse\n{\n    throw new Exception(\"Tenant invalid\"); \n}\n```\n\n- Tenant Database Isolation if isolated = `true`\n\n###### TenantService.cs\n```cs\npublic Tenant CreateTenant(CreateTenantRequest request)\n{\n\n    string newConnectionString = null;\n    if (request.Isolated == true)\n    {\n        // generate a connection string for new tenant database\n        string dbName = \"MultiTenantAppDb-\" + request.Id;\n        string defaultConnectionString = _configuration.GetConnectionString(\"DefaultConnection\");\n        newConnectionString = defaultConnectionString.Replace(\"multi-tenant\", dbName);\n\n        // create a new tenant database and bring current with any pending migrations from ApplicationDbContext\n        try\n        {\n            using IServiceScope scopeTenant = _serviceProvider.CreateScope();\n            ApplicationDbContext dbContext = scopeTenant.ServiceProvider.GetRequiredService\u003cApplicationDbContext\u003e();\n            dbContext.Database.SetConnectionString(newConnectionString);\n            if (dbContext.Database.GetPendingMigrations().Any())\n            {\n                Console.ForegroundColor = ConsoleColor.Blue;\n                Console.WriteLine($\"Applying ApplicationDB Migrations for New '{request.Id}' tenant.\");\n                Console.ResetColor();\n                dbContext.Database.Migrate();\n            }\n        }\n        catch (Exception ex)\n        {\n            throw new Exception(ex.Message);\n        }\n    }\n}\n```\n\n- EF Core Query Filters\n\n###### ApplicationDbContext.cs\n```cs\n// On Model Creating - multitenancy query filter, fires once on app start\nprotected override void OnModelCreating(ModelBuilder builder)\n{\n    builder.Entity\u003cProduct\u003e().HasQueryFilter(a =\u003e a.TenantId == CurrentTenantId);\n}\n```\n\n- Swagger Open API\n\n![image](https://github.com/user-attachments/assets/9ef077ea-901c-473a-a800-d3c79323b876)\n\n\n## Getting Started\n\nTo get started, clone the repository and run the following commands:\n\n```bash\ngit clone https://github.com/ViniciusCestarii/Multi-Tenant-Dotnet-App.git\n```\n\n```bash\ndotnet ef database update --context ApplicationDbContext\n```\n\n```bash\ndotnet run\n```\n\nNow you can navigate to `http://localhost:5284/swagger/index.html` to try the application.\n\n## Prerequisites\n\n- .NET 8\n\n- PostgreSQL\n\n## Running with Docker\n\nTo run the application with Docker, you can use the following command:\n\n```bash\ndocker-compose up\n```\n\nDon't forget to update the connection string in the `appsettings.json` file to point to the PostgreSQL container. (Server=db)\n\nNow you can navigate to `http://localhost:5284/swagger/index.html` to try the application.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fviniciuscestarii%2Fmulti-tenant-dotnet-app","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fviniciuscestarii%2Fmulti-tenant-dotnet-app","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fviniciuscestarii%2Fmulti-tenant-dotnet-app/lists"}