EntityFramework数据库连接字符串重用

EntityFramework数据库连接字符串重用:
<connectionStrings>
  <add name="LegalEntities"
    connectionString="
    //资源模式,另有文件模式
    metadata=res://*/LegalDB.csdl|res://*/LegalDB.ssdl|res://*/LegalDB.msl;
    provider=System.Data.SqlClient;
    provider connection string=&quot;
    //获取要连接的 SQL Server 实例的名称,其返回类型为string
    data source=***;
    initial catalog=***;
    user id=***;
    password=***;
    MultipleActiveResultSets=True;
    App=EntityFramework&quot;"
    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);
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。