开发者

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:

  1. remove these two plain text connection strings

  2. encrypt a single connection string

  3. 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; }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜