【问题标题】:DB connection strings in MVC applications using Entity Framework使用实体框架的 MVC 应用程序中的数据库连接字符串
【发布时间】:2010-12-02 07:22:06
【问题描述】:
我正在使用实体框架开发 MVC 应用程序。
创建 EDMX 后,我注意到 DB 连接字符串位于两个位置 - 我的 Data 类库中的 app.config 文件和我的 Web 应用程序中的 web.config 文件。
我们希望:
去掉这两个纯文本连接字符串
加密单个连接字符串
并在需要时使用我们预先存在的类库来解密连接字符串
我尝试从配置文件中删除一个或另一个连接字符串,但数据库访问失败。为什么需要两个?有没有办法在 MVC - EF 项目中做我们想做的事情,我如何告诉 EF 我们正在做的事情?
谢谢!
【问题讨论】:
标签:
sql-server
asp.net-mvc-2
entity-framework-4
【解决方案1】:
我认为,您可以忽略 EF 项目中的连接字符串,只需从控制器以编程方式设置连接。
public class SomeController : Controller
{
public SomeController()
{
/* Substitute whatever method you want to fetch your data source string here */
/* example assumes plain text from web.config */
string dataSource = ConfigurationManager
.ConnectionStrings["ApplicationServices"]
.ConnectionString;
this.Entities = new SomeEntities(dataSource);
}
private SomeEntities Entities { get; set; }
}