GithubHelp home page GithubHelp logo

tajwal / entityframeworkcoremultitenancy Goto Github PK

View Code? Open in Web Editor NEW

This project forked from khalidabuhakmeh/entityframeworkcoremultitenancy

0.0 0.0 0.0 21 KB

EF Core and Minimal API sample for multi-tenancy example using global filters

C# 93.46% Dockerfile 6.54%

entityframeworkcoremultitenancy's Introduction

ASP.NET Core Minimal APIs and Multi-tenancy

This example shows how you can use EF Core and ASP.NET Core to build multi-tenancy mechanisms into your web applications.

The general flow of the application:

sequenceDiagram
    participant request
    participant middleware
    participant endpoint    
    request->>middleware: /?tenant=Khalid
    middleware->>endpoint: TenantService.Set("Khalid")
    endpoint->>request: Filtered Query by Tenant
Loading

We are utilizing EF Core Query Filters, but they are optional.

    public Database(DbContextOptions<Database> options, ITenantService tenantService)
        : base(options)
    {
        tenant = tenantService.Tenant;
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder
            .Entity<Animal>()
            .HasQueryFilter(a => a.Tenant == tenant)
            .HasData(
                new() {Id = 1, Kind = "Dog", Name = "Samson", Tenant = "Khalid"},
                new() {Id = 2, Kind = "Dog", Name = "Guiness", Tenant = "Khalid"},
                new() {Id = 3, Kind = "Cat", Name = "Grumpy Cat", Tenant = "Internet"},
                new() {Id = 4, Kind = "Cat", Name = "Mr. Bigglesworth", Tenant = "Internet"}
            );
    }

Have fun.

Note

This is using .NET 7 and EF Core 7 bits, but should work with .NET 6 and EF Core 6.

entityframeworkcoremultitenancy's People

Contributors

khalidabuhakmeh avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.