从实体连接字符串中提取连接字符串 [英] Extract connection string from an Entity Connection String

查看:141
本文介绍了从实体连接字符串中提取连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建和ADO.NET实体连接字符串时,您将得到类似

When creating and ADO.NET Entity Connection String you get something like

<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=&quot;server=192.168.0.26;User Id=digitaliv;password=*******;Persist Security Info=True;database=digitaliv&quot;" providerName="System.Data.EntityClient" />

我的问题是如何从中获取真正的内部连接字符串以进行手动SQL连接调用存储过程,自定义sql语句等?具体来说,我需要从中提取这一部分

My question is how do you grab the true inner connection string from that to call make a manual sql connection to call stored procedures, custom sql statements etc.? Specifically I need this part extracted from it

server=192.168.0.26;User Id=digitaliv;password=*******;Persist Security Info=True;database=digitaliv


推荐答案

假设您有一个 ObjectContext 的实例(如果使用的是内置设计器,则上下文是从EF ObjectContext 类)。您可以将 ObjectContext.Connection 属性(它是DbConnection)的值强制转换为 EntityConnection

Assuming you have an instance of the ObjectContext (if you are using the built-in designer, your context derives from the EF ObjectContext class). You can cast the value of the ObjectContext.Connection property (which is a DbConnection) to an EntityConnection.

EntityConnection 类具有属性 StoreConnection 用于连接数据库的实际 DbConnection 。实际上,这是将 ConnectionString 属性设置为您要查找的属性。

The EntityConnection class has a property StoreConnection which is the actual DbConnection used to connect to the database. This one actually has the ConnectionString property set to the one you are looking for.

编辑:一些示例代码(将上下文分配给您的ObjectContext):

Some sample code (Assign context to your ObjectContext):

ObjectContext context = entities;
EntityConnection entityConnection = context.Connection as EntityConnection;
if (null != entityConnection)
{
    Console.WriteLine(entityConnection.StoreConnection.ConnectionString);
}

这篇关于从实体连接字符串中提取连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆