EF代码优先的动态连接字符串

EF代码优先的动态连接字符串

本文介绍了EF代码优先的动态连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前位于Web.config中的连接字符串是:

My current connection string that resides in Web.config is this :

  <add name="PersonDBContext"
       connectionString="Server=111.111.1.11;
       Database=MyProgram;
       User Id=admin;
       Password=12345;
       Integrated Security=False"
       providerName="System.Data.SqlClient" />

我想以动态方式为程序提供相同的连接字符串,这是我的尝试:

I want to give the same connection string to the program in a dynamic way, this is my attempt :

    EntityConnectionStringBuilder csb = new EntityConnectionStringBuilder();

    csb.ProviderConnectionString = "Data Source=111.111.1.11;Initial Catalog=MyProgram;User Id=admin;Password=12345;Integrated Security=False";
    csb.Provider = "System.Data.SqlClient";

    String entityConnStr = csb.ToString();
    return entityConnStr;

这就是我得到的:

您能告诉我我做错了吗?
我是否需要代码优先连接字符串的元数据?谢谢。

Can you tell me what I am doing wrong?And do I need metadata for Code First connection string? Thanks.

编辑:
我认为我不应该使用EntityConnectionStringBuilder或为EntityConnectionStringBuilder类提供元数据。您能告诉我其中一种方法吗?

EDIT :I figured that I either shouldn't use EntityConnectionStringBuilder or I should give a Metadata for the EntityConnectionStringBuilder class. Can you tell me one of the ways to how to do this?

推荐答案

就是这样:

string entityConnStr = "Data Source=111.111.1.11;Initial Catalog=MyProgram;User Id=admin;Password=12345;Integrated Security=False";

这篇关于EF代码优先的动态连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 16:24