从实体框架提供的连接字符串 [英] Provider connection string from Entity Framework

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

问题描述

如果您正在使用的对象CONTEX数据模型(与EDMX文件),它的创建过程中,您可能要指定配置文件中的连接字符串。



连接字符串可惜不是常见的连接字符串,它包含了一些...需要的实体连接的东西。例如与MySQL连接:

 <添加名称=MyDbEntities的connectionString =元= RES:// * /命名空间。 MyDb.csdl |高分辨率://*/Namespace.MyDb.ssdl |高分辨率://*/Namespace.MyDb.msl;提供商= MySql.Data.MySqlClient;提供连接字符串=安培; QUOT;服务器= 172.17.17.154;用户ID =用户;密码=密码;坚持安全信息= TRUE;数据库= MyDatabase的;转换零日期时间=真放; QUOT;的providerName =System.Data.EntityClient/> 



我的问题是,这个连接字符串包含在参数提供商的连接字符串供应商连接字符串。



有关具体的原因,我需要创建一个新的MySqlConnection,无关实体模型。
。对于创建的MySqlConnection,我需要向它提供MySQL的连接字符串 - 这是实体模型的提供程序连接字符串,我知道连接字符串,我需要永远是实体模型相同的连接字符串

但我怎么让供应商连接字符串programmaticaly?我坚持没有成功浏览模型实例...



以下内容:

  ModelInstance.Connection.ConnectionString 

包含类似NAME = TestBotEntities,而不是甚至整个连接字符串。所以我尝试:

  ConfigurationManager.ConnectionStrings [MyDbEntities]的ConnectionString 

但是,一个包含整个实体连接字符串,我只是不知道如何分析它,如何从中只得到供应商的连接字符串。


解决方案

原来有两种方式。



我可以解析通过EntityConnectionStringBuilder实体连接字符串:

 字符串entityConnectionString = ConfigurationManager.ConnectionStrings [MyDbEntities]的ConnectionString; 
串providerConnectionString =新EntityConnectionStringBuilder(entityConnectionString).ProviderConnectionString;



...或者,如果我有可用的特定模型实例,我可以从这里得到它。

 ((System.Data.EntityClient.EntityConnection)ModelInstance.Connection).StoreConnection.ConnectionString; 


If you are using object contex data model (with EDMX file), during its creation you might want to specify the connection string inside your config file.

The connection string is unfortunately not the common connection string as it contains some ...things needed for the entity connections. Example with MySql connection:

<add name="MyDbEntities" connectionString="metadata=res://*/Namespace.MyDb.csdl|res://*/Namespace.MyDb.ssdl|res://*/Namespace.MyDb.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=172.17.17.154;User Id=user;password=password;Persist Security Info=True;database=MyDatabase;Convert Zero Datetime=true&quot;" providerName="System.Data.EntityClient" />

The problem I have is that this connection string contains the connection string of the provider in the parameter "provider connection string".

For a specific reason, I need to create a new MySqlConnection, unrelated to the entity model. For creating the MySqlConnection, I need to provide it the mysql connection string - which is the provider connection string for the entity model and I know the connection string I need is always the same connection string for the entity model.

But how do I get the provider connection string programmaticaly? I was stuck with browsing the model instance with no success...

The following:

ModelInstance.Connection.ConnectionString

contains something like "name=TestBotEntities", not even the whole connection string. So I tried:

ConfigurationManager.ConnectionStrings["MyDbEntities"].ConnectionString

but that one contains the whole entity connection string and I just don't know how to parse it, how to get only the provider connection string from it.

解决方案

Turns out there are two ways.

I could parse the entity connection string via the EntityConnectionStringBuilder:

string entityConnectionString = ConfigurationManager.ConnectionStrings["MyDbEntities"].ConnectionString;
string providerConnectionString = new EntityConnectionStringBuilder(entityConnectionString).ProviderConnectionString;

...or if I have the specific model instance available, I can get it from here.

((System.Data.EntityClient.EntityConnection)ModelInstance.Connection).StoreConnection.ConnectionString;

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

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