Question

I have an ASP.Net MVC project that uses EF. I develop this application at work computer and at my home computer. The following is in my Web.config. How could I change the EDMX with the proper connection string for each office? So far, I have to drop the EDMXs and re-add them and comment out other connection strings. Thanks.

    <connectionStrings>
    <clear/>
    <!-- At Home connections -->
    <add name="App1Entities" connectionString="metadata=res://*/Models.App1Model.csdl|res://*/Models.App1Model.ssdl|res://*/Models.App1Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=my-pc\SqlExpress;initial catalog=App1Database;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    <add name="Service1Entities" connectionString="metadata=res://*/Models.Service1Model.csdl|res://*/Models.Service1Model.ssdl|res://*/Models.Service1Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=my-pc\SqlExpress;initial catalog=ServiceDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

    <!-- At Work connections 
    <add name="App1Entities" connectionString="metadata=res://*/Models.App1Model.csdl|res://*/Models.App1Model.ssdl|res://*/Models.App1Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=sqlServer1;initial catalog=App1Database;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    <add name="Service1Entities" connectionString="metadata=res://*/Models.Service1Model.csdl|res://*/Models.Service1Model.ssdl|res://*/Models.Service1Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=sqlServer2;initial catalog=ServiceDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    -->
</connectionStrings>
Was it helpful?

Solution

You need multiple connection strings, but you don't have to put a switch in your code; use

<connectionStrings configSource="MyLocalConnectionStrings.config" />

see MSDN

You'd need to maintain MyLocalConnectionStrings.config in the same dir as web.config, and have a different version for each development environment.

OTHER TIPS

Assuming the schemas are the same in the two databases, it should just be a matter of replacing the connection string in web.config. So I really don't understand what your confusion is.

With Visual Studio, you can use Config Tranforms to generate different config settings based on the mode ("Release" and "Debug" are setup by default, you can add others) selected in Configuration Manager.

Set up a transform for the connection string, then when you publish the site to Release mode, the correct connection string will be in there.

Add a mode for "Home" or something, with the appropriate connection string given your environment at home.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top