一个网站的多个数据库,数据库切换 [英] Single website multiple databases, database switching

查看:187
本文介绍了一个网站的多个数据库,数据库切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了我公司的产品数据库内容管理系统(CMS)。该CMS是基于在混合许多自定义页面和动作asp.net脚手架等。我们目前有7款产品,所有这一切都共享同一个数据库模式(实体框架模型第一次)和所有在CMS完美运行。问题是,每次我们得到一个新的产品,我们必须复制CMS和更改连接字符串的app.config 来指向正确的数据库,以便与工作新的数据库。虽然这工作,它变得麻烦,以保持和我们获得更多的产品将彻底失败我们。

I have created a content management system (CMS) for my company’s product databases. The CMS is based on asp.net scaffolding with many custom pages and actions mixed in. We have 7 products currently, all of which share the same database schema (Entity Framework model-first) and all run perfectly in the CMS. The issue is that every time we get a new product we must clone the CMS and change the connection string in the app.config to point to the correct database in order to work with the new database. While this works, it’s becoming bothersome to maintain and will fail us completely as we acquire more products.

我想这样做的是有其中一个用户被定向到登录在一个集中的登陆页面,再给予连接到和编辑根据自己的选择特定产品的选项。这个想法是,我们将有一个CMS网站,其中将能够根据用户数据库之间进行切换。它不是所有的产品数据库的整合到一个单一的主产品数据库中的一个选项。

What I would like to do is have a centralized landing page where a user is directed to log in, then given the option to connect to and edit a specific product based on their selection. The idea is that we would have one CMS site which would be able to switch between the databases depending on the user. It is not an option to combine all of the product database in to a single master product database.

我不知道从哪里开始实现这个目标,如果这是连正确的计划,以实现我有一个CMS维护,并要寻找一个在此提供一些指导的目标。

I am not sure where to start to achieve this goal, or if this is even the correct plan to achieve my goal of having a single CMS to maintain, and am looking for some guidance in this.

推荐答案

假设你的数据库结构是相同的,你可以使用一个工厂方法在任何地方你会得到你的实体上下文的实例,并把逻辑在那里抢到了正确的连接字符串(或计算,如果有,你可以使用一个命名约定)。像这样的东西可能例如工作:

Assuming that your database structures are identical, you could use a factory method anywhere you get an instance of your entity context and put logic in there to grab the correct connection string (or calculate it if there's a naming convention that you could use). Something like this might work for example:

    public static MyDatabaseEntities CreateEntityContext(string productName)
    {
        string connectionString = null;
        switch (productName.Trim().ToLower())
        {
            case "apples":
                connectionString = ConfigurationManager.ConnectionStrings["MyDatabase_Apples"].ConnectionString;
                break;
            case "pears":
                connectionString = ConfigurationManager.ConnectionStrings["MyDatabase_Pears"].ConnectionString;
                break;
            default:
                connectionString = ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString;
                break;
        }
        return new MyDatabaseEntities(connectionString);
    }

然后在任何地方使用这个方法需要传递你已经着陆页计算产品名称你的CRM数据上下文的实例。

Then use this method anywhere you need an instance of your CRM data context passing in the product name that you've calculated on your landing page.

这篇关于一个网站的多个数据库,数据库切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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