Entity Framework - layered design - Where to put connectionstring?

折月煮酒 提交于 2019-11-28 08:42:47

You can copy the connection string created in the App.Config of the DAL assembly into the connectionStrings section of the web.config.

You can keep the connection string in the assembly dll but you shouldn't deploy it with the website.

You will need to copy the entire connection string. It should look like this:

<add name="DataEntities" connectionString="metadata=res://*/DataModel.csdl|res://*/DataModel.ssdl|res://*/DataModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=xxx;Initial Catalog=xxx;User Id=xxx;Password=xxx;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />

It should include all the information on where the mapping files exist.

In my case, although I am using L2S instead of L2E, but the recommendation should stand. I have a generalize Config library that feeds from an XML file. When a data context is required, every data object has a method like the following. Granted, it could be templated easy enough, if you prefer.

private static string _conStr = null;
private static CalendarsAndListsDataContext GetDataContext()
{
    if (_conStr == null)
    {
        _conStr = ConfigurationLibrary.Config.Settings().GetConnectionString("liveConString");
    }

    return new CalendarsAndListsDataContext(_conStr);
}

Now, biggest downside is connection string changes require an application restart, but in my case, that is not an issue.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!