如何在MVC 4上管理多个数据库 [英] How to manage multiple database on MVC 4

查看:91
本文介绍了如何在MVC 4上管理多个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在浏览互联网上的不同网站,以解决我的问题。我需要为Web应用程序管理多个数据库。假设我有2个客户端,他们将拥有自己的数据库(这是2个数据库),我还需要检查他们的帐户是否存在于我的主数据库中。代码基本上将目标主数据库检查其帐户是否存在,然后以编程方式更改连接/或将它们指向他们拥有的数据库。我在检查帐户后尝试更改web.config,但每次用户尝试登录时,这似乎都是一个糟糕的程序流,Web配置将被更改,登录的用户将被定向到新的数据库连接。



我希望听到你们的回复。

I've been looking through different sites on the internet for a solution to my problem. I need to manage multiple databases for a web app. Let's say that I have 2 clients, they will be having their own database (so that's 2 database), also i need to check if their account exist on my primary database. The code will basically target the primary database to check if their account exist, then programatically change the connection/ or point them to the database that they own. I'm tried to alter the web.config after checking their accounts, but this seems to be a bad program flow for each time a user attempts to log in, the web config will be altered and the logged in users will be directed to the new database connection.

I hope to hear a good answer from you guys.

推荐答案

你是什么正在处理的是多租户。与您的问题没有直接关系,但这是一本关于多租户环境中最佳实践的好书。



http://msdn.microsoft.com/en-us/library/ff966499.aspx [ ^ ]



回复你问题,我不认为为各种租户维护静态连接字符串是个好主意。可能你明天可以有10个甚至100个租户。



我的建议是实现一个ConnectionStringManager类,它可以为你提供特定客户端的连接字符串。
What you are dealing with is Multi Tenancy. Not directly related to your question, but here is a good book on best practices in multi tenant environment.

http://msdn.microsoft.com/en-us/library/ff966499.aspx[^]

Coming back to you question, I don't think it's a good idea to maintain static connection strings for various tenants. Potentially, you can have 10 or even 100 tenants tomorrow.

My suggestion would be do implement a ConnectionStringManager sort of class which can give you the connection string for a specific client.


Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
            ConnectionStringsSection section = config.GetSection("connectionStrings") as ConnectionStringsSection;
            var connection = WebConfigurationManager.ConnectionStrings["ClientConnection"].ConnectionString;
            string[] conn = connection.Split(';');
            string dataSource = conn[0];
            string initialCatalog = conn[1];
            string persistentSecInfo = conn[2];
            string userID = conn[3];

            string dbName=initialCatalog.Substring(16,9);

            if (!dbName.Equals(officeID))
            {
                connection = connection.Replace(dbName, officeID);
                section.ConnectionStrings["ClientConnection"].ConnectionString = connection;
                config.Save();
            }





这是我用来根据officeID更改连接的代码(指代客户端的数据库名称) ),我将初始目录的值替换为指向我需要的数据库。



有任何建议或意见吗?



This is the code I use to change my connection based on the officeID (which refers to the client's database name), I replace the value of the Initial Catalog to point to the database I need.

Any suggestions or comments?


这篇关于如何在MVC 4上管理多个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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