首先使用Entity Framework 4.1代码时出现此错误。我找不到任何确切使用的来源。
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="Data Source=myserver.com;Initial Catalog=MyDataBase;Persist Security Info=True;User ID=username;Password=password"" providerName="System.Data.EntityClient" />
答案 0 :(得分:68)
对于EF Code First,如果使用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" />
答案 1 :(得分:1)
如果要为Code First Entity Framework创建动态连接字符串,则只能使用下面给出的Sql Connection String Builder。
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();
}