简体   繁体   English

针对登录用户动态创建多个数据库体系结构的DbContext

[英]Create DbContext dynamically for multiple database architecture with repect to logged-in user

I am working on multi layered architecture with supporting multiple database per tenant in MVC. 我正在研究多层体系结构,在MVC中每个租户支持多个数据库。 I need to create DBContext depending on the user logged in into data layer. 我需要根据登录到数据层的用户来创建DBContext。 How should I create generic DBContext and set connection string according to user? 如何创建通用DBContext并根据用户设置连接字符串?

Following is code to get connection string from Host Databsae and set it to Client Database. 以下是从Host Databsae获取连接字符串并将其设置为Client Database的代码。 (In data/repository layer) (在数据/存储层中)

private static string GetConnectionString()
{
    int tenantId;
    HostDBContext hostContext = new HostDBContext();

    //Get Tenant and set ID to get required connection string
    tenantId = 1; //Get Tenant Id from session

    var tenantDetails = hostContext.TenantDetails.Find(tenantId);

    if (tenantDetails != null)
    {
        return tenantDetails.connectionstring;
    }

    return "default connection string";
}

You're on the right track, but it won't be a generic DbContext , just one that with a connection string that's set at runtime. DbContext正确的轨道上,但这不是通用的 DbContext ,而只是一个在运行时设置了连接字符串的DbContext So instead of GetConnectionString, you want to create a DbContext factory that requires the tenant Id to return your new DbContext("connectionString"). 因此,您要创建一个DbContext工厂而不是GetConnectionString,该工厂要求租户ID返回新的DbContext(“ connectionString”)。

Let's call it TenantDbContext : 我们称之为TenantDbContext

public class TenantDbContext : DbContext
{
    public TenantDbContext(string connectionString) : base(connectionString)
    {
    }
// Or
    public TenantDbContext(int tenantId) : base(GetConnectionString(tenantId))
    {
    }
}

if using a factory: 如果使用工厂:

public class TenantDbContextFactory : IDbContextFactory<TenantDbContext, int>
{
    public TenantDbContext Create(int tenantId)
    {
        var connectionString = GetConnectionString(tenantId);
        return new TenantDbContext(connectionString);
    }
    // ... rest of factory
}

public interface IDbContextFactory<TContext, TKey> where TContext :DbContext
{
     TContext Create(TKey key);
}

If you're using a dependency injection container, you can also wire it up to return the TenantDbContext based on the tenant Id. 如果您使用的是依赖项注入容器,则还可以将其连接起来以根据租户ID返回TenantDbContext

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取当前登录用户的详细信息? - How to get current logged-In User details? 如何访问已登录用户的属性? - How to access the properties of a logged-in user? 将“飞行”分配给已登录/注册的用户? - Assign 'flight' to a logged-in/ registered user? 阻止已登录用户的登录 - Prevent login of already logged-in user Kentico BizForm API - 如何使用登录用户从后端“匿名”创建该表单? - Kentico BizForm API - how to create that form "anonymously" from the backend with logged-in user? 如何在 Blazor Signalr 中为多个 Window 中的同一登录用户刷新数据 - How To Refresh Data For Same Logged-in User In Multiple Window In Blazor Signalr 使用登录用户凭据运行 .NET 控制台应用程序 - Run .NET Console Application with logged-in user credentials 如何从登录用户添加新用户? - How to add new users from a logged-in user? Keycloak:如何为登录的Windows用户自动进行身份验证? - Keycloak: How to make authentication automatic for logged-in Windows user? 如何在WPF应用程序中检查登录用户是交互式的还是闲置的 - How to check whether the logged-in user is interactive or idle in wpf application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM