Entity Framework Code First and Connection String Issue

30,518

Solution 1

For EF Code First you can use ordinary connection string if you are using SQL Server.

<add name="DataContext" connectionString="Data Source=myserver.com;Initial Catalog=MyDataBase;Persist Security Info=True;User ID=username;Password=password"  providerName="System.Data.SqlClient" />

Solution 2

If you are creating dynamic connection string for Code First Entity Framework than you can do using only Sql Connection String Builder as given below.

 public static string  DynamicConnectionString(SqlConnectionStringBuilder builder)
 {
    SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
    builder.DataSource = "ServerName";
    builder.InitialCatalog = "DatabaseName";
    builder.UserID = "UserId";
    builder.Password = "Password";
    builder.MultipleActiveResultSets = true;
    builder.PersistSecurityInfo = true;    
    return builder.ConnectionString.ToString();
}
Share:
30,518
Mike Flynn
Author by

Mike Flynn

I am the founder and CEO of Exposure Events. Exposure Events is a tournament and league management system delivering online scheduling and conflict checker, live results, apps, free directory and more.

Updated on July 20, 2020

Comments

  • Mike Flynn
    Mike Flynn almost 4 years

    I am getting this error when using Entity Framework 4.1 code first. I can not find any sources of what exactly to use.

    Unable to load the specified metadata resource.
    
    <add name="DataContext" connectionString="metadata=res://*/GrassrootsHoopsDataContext.csdl|res://*/GrassrootsHoopsDataContext.ssdl|res://*/GrassrootsHoopsDataContext.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=myserver.com;Initial Catalog=MyDataBase;Persist Security Info=True;User ID=username;Password=password&quot;" providerName="System.Data.EntityClient" />