Question

I have modified the web.config connection string. However, during debug, I still see the old connection string.

So, I have commented (and deleted) out the old connection string but, and added a new connection resource via server explorer. On testing the connection via wizard on the left panel in server explorer- it says connected.

After following this wizard, when I visit the web.config I dont see the new connection string.

Question: I suspect, I am not following the steps to add a connection string - how can I add or update the connection string from the designer (in the designer properties panel, edit is greyed out, output type is build to assembly, and right clicking only gives me options to add entities etc, deleting the string and running the app, doesn't prompt for connection string wizard)

Below is the string -

<connectionStrings><add name="MaintRecordsDB_v1" connectionString="metadata=res://*/Models.DB.Model.csdl|res://*/Models.DB.Model.ssdl|res://*/Models.DB.Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=xxx.sample.net;initial catalog=MainDB;user id=maintRoot;password=hidden;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" /><add name="MainDBentities" connectionString="metadata=res://*/Models.DB.Model.csdl|res://*/Models.DB.Model.ssdl|res://*/Models.DB.Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=windflower.arvixe.com;initial catalog=MX_Dashboard;user id=maintRoot;password=hidden;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" /></connectionStrings>

Edit Question 2: How can I add another EF connection string for e.g. MaintDB2 using designer, and where do I update to this manually.

Was it helpful?

Solution

Without knowing what your context class looks like, say your DbContext class, if it was generated and assuming it's partial, you could try to add another partial class part to it with a constructor that takes a named connection string as an argument.

First add a named connection to your app.config/web.config:

<connectionStrings>
...
<add name="MyOtherConnection" connectionString="metadata=res://*/blahblahblah;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=ABunchOfOtherStuff;"
  providerName="System.Data.EntityClient" />
</connectionStrings>

Then add a matching partial class in another (non-generated) file with a constructor to take a connection string name:

// the class name must match the name of your existing context
public partial class MyContext : DbContext
{
    public MyContext(string connectionStringName) : base("name=" + connectionStringName)
    {
    }
}

Then use your context by passing in the name of the connection string, demonstrated by some useless code:

// ...
using (var context = new MyContext("MyOtherConnection"))
{
   var id = 1;
   var collection = context.MyEntities.Where(a => a.ID == id).ToList();
}

OTHER TIPS

In MVC several things are based on Convention. It prefers convention over configuration. By convention there should be a link between the two things. The class name of DbContext match the connection string for it to work correctly by convention

  1. The class inheriting from DbContext say

    public class DbPersonContext : DbContext { ... }

  2. must have a connection string of the name Person

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