连接到数据库多租户应用程序? [英] Connecting to database for Multi-Tenant application?

查看:256
本文介绍了连接到数据库多租户应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在创建多租户asp.net应用程序的过程。

I am in the process of creating a Multi-Tenant asp.net application.

我单独MSSQL数据库或模式之间进行选择。

I am deciding between separate MSSQL databases or schemas.

不过,我找不到我如何能更改数据库(理想情况下)或用户帐户动态,视用户应该连接到哪个的任何信息。

However, I cannot find any information on how I can change the database (ideally) or user account dynamically, depending on which one the user should connect to.

我将最有可能有一个基本表定义了用户应连接到的数据库。

I will most likely have a Base Table which defines which database the user should connect to.

这样做使用LINQ to SQL是容易的,但我没有到处使用LINQ,因为表是颇具动感和架构是喜欢经常改变。

Doing this using Linq to SQL is easy but I am not using Linq everywhere because the tables are quite dynamic and the schema is like to change very often.

什么是这样做的最好的方法?我很高兴地看到使用模式,但我不希望它变得非常混乱,但我也需要以同样的方式做了一个类似的方法,但不知何故冒充该用户皮卡默认架构。

What is the best method of doing this? I am happy to look at using Schemas, but I don't want it to become very messy but I would also need to do a similar approach but somehow impersonate that user in the same way to pickup a default schema.

我知道你可以动态地改变web.config中的连接字符串,但我已经试过了,它在物理更改文件内容也刷新应用程序池,并导致了我很多其他问题。

I know you can dynamically change the web.config connection string but I have tried that and it physically changes the file contents which also refreshes the app pool and causes me lots of other issues.

感谢

推荐答案

如果您已建立了多个租户多个数据库,您可以根据租户创建连接字符串。

If you have set up multiple database for multiple tenants, you can create connection string based on the tenant.

您可以简单地添加过载到你的数据库上下文构造接受连接字符串输入:

You can simply add an overload to your db context constructor that accepts connection string as input:

public partial class SampleDbEntities
{
    public SampleDbEntities(string connectionString)
        : base(connectionString)
    {
    }
}

那么无论你需要创建你的数据库环境的实例,用这个重载并注入合适的用户名密码在连接基于您的租户检测策略字符串。

Then wherever you need to create an instance of your db context, use this overload and inject suitable username and password in the connection string based on your tenant detection strategy.

例如,当您的连接字符串看起来是这样的:

For example when your connection string looks like this:

var connectionTemplate =
    @"metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;" +
    @"provider=System.Data.SqlClient;" +
    @"provider connection string=""data source={0};" +
    @"initial catalog={1};" +
    @"persist security info=True;" +
    @"user id={2};" +
    @"password={3};" +  
    @"MultipleActiveResultSets=True;App=EntityFramework""";

string connection = string.Format(connectionTemplate, 
    @"(localdb)\v11.0", @"TestDB", @"user1" , @"password1");

var db = new SampleDbEntities(connection);

注意:


  • 创建基于连接字符串连接字符串模板,在你的的web.config

  • Create connection string template based on the connection string which is in your web.config.

这篇关于连接到数据库多租户应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆