简体   繁体   English

Asp.net MVC2-多个数据库

[英]Asp.net MVC2 - Multiple databases

I have a database called "Config" where I have a table called "Customer" In the Customer table I hold the login credentials, along with a connection String to the client's database. 我有一个名为“ Config”的数据库,其中有一个名为“ Customer”的表。在Customer表中,我保存了登录凭据以及与客户端数据库的连接字符串。

Inside of my controller I'm checking to see what customer database i'm querying 在控制器内部,我正在检查要查询的客户数据库

        String connectionString = "";
        if (id == 1)
            connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Client1.mdf;Integrated Security=True;User Instance=True";
        else if (id == 2)
            connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Client2.mdf;Integrated Security=True;User Instance=True";

        using (TestDbaseDataContext db = new TestDbaseDataContext(connectionString))
        {
            var searchRecords = db.SearchRecords;
            return View(searchRecords.ToList());
        }

There will be an undetermined number of client's using separate databases, these connection strings will change based on archived status, and other factors. 使用单独的数据库的客户端数量不确定,这些连接字符串将根据存档状态和其他因素而变化。 I don't think hard coding these strings in app.config will do what I need. 我认为在app.config中对这些字符串进行硬编码不会满足我的需要。

I have a "Master" database, that holds all of the client's configuration settings, along with the client's database connection string. 我有一个“主”数据库,其中包含所有客户端的配置设置以及客户端的数据库连接字符串。 The mater database is only used to mitigate which client database to search. 主数据库仅用于减轻要搜索的客户端数据库。 I store the connection strings in the master database so I can pass that connection string for the logged in customer to query the correct database. 我将连接字符串存储在master数据库中,因此我可以为已登录的客户传递该连接字符串,以查询正确的数据库。 It is a requirement to have each client in it's own database 要求每个客户都在自己的数据库中

How would you store this connection string so I can pass to my datacontext when needed? 您将如何存储此连接字符串,以便在需要时可以传递给我的datacontext? Should it be stored in a session variable, or should I query the Config database Customer table in each controller method? 应该将其存储在会话变量中,还是应该在每种控制器方法中查询Config数据库的Customer表?

I've set up a similar "siloed" multi-client application before. 之前,我已经建立了一个类似的“隔离”多客户端应用程序。 This is a good case for a session variable. 对于会话变量,这是一个很好的情况。 Each user relates to a client, which has its own database. 每个用户都与一个客户端有关,该客户端具有自己的数据库。 On login, each user should get a "context" object in Session state containing all necessary identifying data, including database connection string. 登录时,每个用户都应在会话状态下获得一个“上下文”对象,其中包含所有必需的标识数据,包括数据库连接字符串。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM