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

1 Answer 1

1

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") 
      { 
      } 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.