码迷,mamicode.com
首页 > 数据库 > 详细

EntityFramework数据库连接字符串重用

时间:2014-09-23 11:07:14      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:blog   io   文件   数据   sp   cti   on   c   log   

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);
}

EntityFramework数据库连接字符串重用

标签:blog   io   文件   数据   sp   cti   on   c   log   

原文地址:http://www.cnblogs.com/EdwardJack/p/3987639.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!