views:

2513

answers:

2

Hi,

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

I want my website to be responsible of specifying the connectionstring for my entity model. How do I do this?

I am using a singleton method to get to my entity repository, which is located inside the datalayer.

Thanks

+2  A: 

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.

Serapth
I like the idea of how you separate your configuration in an additional project. But for this application I want my website to be in charge of the connectionstring.
Peter
Well, there is no reason why your Cofiguration Layer has to be read only. You could make it read/write, so then you could do something like COnfigurationLibrary.Config.AddSetting("ConnectionString","MyString"). Then your website could still drive the connection string.
Serapth
+6  A: 

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.

TGnat
I recreated my entity model and tried again and now it does work, thanks!!!
Peter