DB connection strings in MVC applications using Entity Framework
I am working on an MVC application using Entity Framework.
After creating an EDMX, I noticed the DB connection string is located in TWO places - an app.config file in my Data class library, and a web.config file in my web application.
We want to:
remove these two plain text connection strings
encrypt a single connection string
and use our pre-existing class library to decrypt the connection string when needed
I tried removing one or the other connection string from the config files, and DB ac开发者_Go百科cess fails. Why are TWO required? And is there any way to do what we want in an MVC - EF project, and how would I tell EF that is what we are doing?
Thanks!
You can ignore the connection string in your EF project, I think, and just set the connection programmatically from your controller.
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; }
}
精彩评论