EntityFramework数据库连接字符串重用:
<connectionStrings>
<add name="LegalEntities"
connectionString="
//资源模式,另有文件模式
metadata=res://*/LegalDB.csdl|res://*/LegalDB.ssdl|res://*/LegalDB.msl;
provider=System.Data.SqlClient;
provider connection string="
//获取要连接的 SQL Server 实例的名称,其返回类型为string
data source=***;
initial catalog=***;
user id=***;
password=***;
MultipleActiveResultSets=True;
App=EntityFramework""
providerName="System.Data.EntityClient"/>
</connectionStrings>
自己创建EntityConnection,并传递给ObjectContext。
public CNBlogsObjectContext()
: base(BuildConnection("CNBlogsDB"), "club_CNBlogsContainer")
{
}
static EntityConnection BuildConnection(string connectionStringName)
{
string connectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
MetadataWorkspace workspace = new MetadataWorkspace(new string[] { "res://*/" },
new Assembly[] { Assembly.GetExecutingAssembly() });
SqlConnection sqlConnection = new SqlConnection(connectionString);
return new EntityConnection(workspace, sqlConnection);
}