user1960836
user1960836

Reputation: 1782

connection strings parameter name=

  <connectionStrings>
<add name="MyContext" providerName="MySql.Data.MySqlClient" connectionString="server=localhost;
  port=3306;database=mycontext;uid=root;password=********"/>

In the above code the name attribute says my context, and the database attribute is also set to mycontext. What does the name attribute do, will I use it from some external code? I am trying to create a connection to a local database for the first time, and I don't know what I should do with the name attribute. Can someone please provide an example? I have tried to google "mysql connectionstring" (among others) and very few of them actually have the name attribute in the connectionstring.

I am using vs2013 and trying to set up a connection string with entity framework

Upvotes: 0

Views: 79

Answers (1)

Mithrandir
Mithrandir

Reputation: 25397

The name attribute is there, so you can refer to the connection string from code or other sections in the app.config or web.config, as needed.

In C# it might look somethong like this:

var connectionString = ConfigurationManager.ConnectionStrings["MyContext"].ConnectionString;

or, if you are using EntityFramework:

public class SomeContext : DbContext 
{ 
      public SomeContext() 
         : base("name=MyContext") 
      { 
      } 
}

Upvotes: 1

Related Questions