生成的连接在ASP.NET MVC中动态字符串 [英] Generate connection string dynamically in ASP.NET MVC

查看:397
本文介绍了生成的连接在ASP.NET MVC中动态字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我工作的ASP.NET MVC项目。目前我使用模式第一种方法,在这里我用手动使用ADO.NET模型添加数据库。目前,我有4个数据库和我在web.config文件4连接字符串。

Hi I am working on ASP.NET MVC project. Currently I am using Model first approach, where i used to add database manually by using ADO.NET model. Currently I have 4 database and I have 4 connection strings in web.config file.

这是精细到现在,因为我工作的开发环境。但现在我需要把我的code生活和问题,在现场我们有像40至50个数据库。

It was fine till now, since I was working on development environment. But now I need to move my code to live and problem is, in live we have like 40 to 50 databases.

所以,我应该做的是,动态生成的连接字符串,如果用户想要连接到特定的数据库。

So what I should do is, generate connection string dynamically when user wants to connect to particular database.

我的存储过程这个返回的连接字符串和数据库名称。

I have stored procedure for this which returns connection string and database name.

例如,如果我有像DB1,DB2,DB3和DB4 4数据库名称,我需要这个数据库的名字和我的存储过程的结果数据库名称进行比较,如果两者相等,则生成的连接字符串等于数据库名。

For example if I have 4 database name like db1, db2, db3 and db4, I need to compare this database name with my stored procedure results database name and if both are equal, then generate connection string equal to that database name.

和我也需要把这个会议有一次,我产生的连接字符串,所以没必要再生成连接字符串为特定的会话。

And also I need to put this in session once i generate connection string, so no need to generate connection string again for particular session.

有人可以帮助我在这?

推荐答案

EF 的DbContext 构造函数的参数将连接字符串或连接字符串本身的名称。所以在生成任何类型的连接字符串没有问题,且提供此创建DdContext时。

EF DbContext as a constructor parameter takes a name of a connection string or connection string itself. So there is no problem in generating any kind of connection string and supply this when creating DdContext.

在我们的应用中有很多住户,并有每个租户的数据库。对于每一个要求,我们查找它是租户和设置数据库提供连接字符串租户自己的数据库。

In our application we have many tenants and have a database per tenant. For every request we lookup what tenant it is and from Settings DB provide a connection string to tenant's own database.

我没有曾与Ado.Net,但是从我在谷歌看,这是非常相似的(或基于)实体框架。所以到特定的实现,必须有一种方式来提供连接字符串数据库上下文outwith 的web.config

I've not worked with Ado.Net, but from what I see in Google, this is very similar (or based on) to Entity Framework. So down to your particular implementation, there must be a way to provide connection string to database context outwith web.config.

这篇关于生成的连接在ASP.NET MVC中动态字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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