当创建和ADO.NET实体连接字符串你得到的东西
<add name="MREntities" connectionString="Metadata=res://*/App_Code.MembershipAndRoll.MembershipAndRollEntities.csdl|res://*/App_Code.MembershipAndRoll.MembershipAndRollEntities.ssdl|res://*/App_Code.MembershipAndRoll.MembershipAndRollEntities.msl;provider=MysqL.Data.MysqLClient;provider connection string="server=192.168.0.26;User Id=digitaliv;password=*******;Persist Security Info=True;database=digitaliv"" providerName="System.Data.EntityClient" />
我的问题是如何抓住真正的内部连接字符串来调用make手动sql连接来调用存储过程,自定义sql语句等?具体来说,我需要从中提取此部分
server=192.168.0.26;User Id=digitaliv;password=*******;Persist Security Info=True;database=digitaliv
解决方法
假设你有一个ObjectContext的实例(如果你使用内置的设计器,你的上下文来自于EF ObjectContext类).您可以将ObjectContext.Connection属性(即DbConnection)的值转换为EntityConnection.
EntityConnection类有一个属性StoreConnection,它是用于连接到数据库的实际DbConnection.这个实际上将ConnectionString属性设置为您要查找的属性.
编辑:一些示例代码(为您的ObjectContext分配上下文):
ObjectContext context = entities; EntityConnection entityConnection = context.Connection as EntityConnection; if (null != entityConnection) { Console.WriteLine(entityConnection.StoreConnection.ConnectionString); }