实体框架代码优先和连接字符串问题

时间:2011-08-21 00:34:38

标签: entity-framework entity-framework-4.1 ef-code-first connection-string

首先使用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=&quot;Data Source=myserver.com;Initial Catalog=MyDataBase;Persist Security Info=True;User ID=username;Password=password&quot;" providerName="System.Data.EntityClient" />

2 个答案:

答案 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();
}