Entity Framework - Layered Design - Where to put a connectionstring?

I am using a layered architecture with the Entity Framework as my datalayer with a bunch of repositories on top that contain Linq-To-Entities queries. The data tier is one project, next to which I have a service tier and an interface, which is a website.

I want my site to be responsible for specifying a connection string for my entity model. How to do it?

I use the singleton method to access my object repository, which is inside the datalayer.

thanks

+7
c # entity-framework data-access-layer n-tier
source share
2 answers

You can copy the connection string created in the App.Config of the DAL node to the connectionStrings section of the web.config file.

You can save the connection string in the dll assembly, but you should not deploy it with a 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 contain all the information about where the mapping files exist.

+9
source share

In my case, although I use L2S instead of L2E, a recommendation should be made. I have a generic Config library that feeds from an XML file. When a data context is needed, each data object has a method such as the following. Of course, if you want, you can easily mask the template.

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

Now the biggest drawback is the connection string changes that require the application to restart, but in my case this is not a problem.

+3
source share

All Articles