How to update an Entity Framework connection string

I changed the connection string web.config. However, during debugging, I still see the old connection string.

So, I commented (and deleted) from the old connection string, but added and added a new connection resource through the server explorer. When testing the connection through the wizard in the left pane in the server explorer - it says that it is connected.

After completing this wizard, when I visit web.config, I do not see a new connection string.

Question . I suspect that I am not following the steps to add a connection string - how can I add or update a connection string from the designer (in the designerโ€™s properties panel, the editing is grayed out, the output type is assembly, and right-clicking gives me the ability to add objects and t .d. deleting the line and launching the application, does not ask the connection string master)

Below is the line -

<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>

Change Question 2: . How to add an extra EF connection string, for example. MaintDB2using the constructor, and where I update it manually.

+4
source share
2 answers

, , DbContext, , , .

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>

( ) , :

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

, , :

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

MVC . . . DbContext

  • , DbContext,

    DbPersonContext: DbContext  {...}

+1

Source: https://habr.com/ru/post/1529615/


All Articles