user266909
user266909

Reputation: 1863

asp.net mvc entity framework with multiple databases

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>

Upvotes: 0

Views: 1168

Answers (3)

Kirk B.
Kirk B.

Reputation: 456

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.

Upvotes: 0

Faust
Faust

Reputation: 15404

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.

Upvotes: 0

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93424

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.

Upvotes: 1

Related Questions