简体   繁体   English

来自 Entity Framework 的提供程序连接字符串

[英]Provider connection string from Entity Framework

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.如果您正在使用对象上下文数据模型(带有 EDMX 文件),则在创建过程中您可能需要在配置文件中指定连接字符串。

The connection string is unfortunately not the common connection string as it contains some...things needed for the entity connections.不幸的是,连接字符串不是通用连接字符串,因为它包含一些……实体连接所需的东西。 Example with MySql connection: MySql 连接示例:

<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.出于特定原因,我需要创建一个与实体模型无关的新 MySqlConnection。 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.为了创建 MySqlConnection,我需要为其提供 mysql 连接字符串——这是实体模型的提供者连接字符串,我知道我需要的连接字符串始终与实体模型的连接字符串相同。

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.包含类似“name=TestBotEntities”的内容,甚至不包含整个连接字符串。 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:我可以通过 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;

I have to confess I cheated because I didn't want the dependency on EF's assembly in the project I was working on.我不得不承认我作弊了,因为我不想在我正在处理的项目中依赖 EF 的程序集。 So I used a regex to "parse" the 'provider connection string' value thusly:因此,我使用正则表达式来“解析”“提供者连接字符串”值:

provider connection string="(.+)";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM