Search code examples
entity-frameworkentity-framework-4asp.net-mvc-4edmxedmx-designer

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>

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.