在 C# 中在运行时设置实体框架连接字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14673559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:38:44  来源:igfitidea点击:

Set Entity Framework Connection String at Runtime in C#

c#entity-framework

提问by JQuery Mobile

I need to set my Entity Framework connection string at runtime. Right now, I have the following:

我需要在运行时设置我的实体框架连接字符串。现在,我有以下几点:

string connectionString = "metadata=res://*/DataModels.CustomerDataModel.csdl|res://*/DataModels.CustomerDataModel.ssdl|res://*/DataModels.CustomerDataModel.msl;provider=System.Data.SqlClient;provider connection string="data source=tcp:{serverName},{portNumber};initial catalog={databaseName};user id={username};multipleactiveresultsets=True;application name=EntityFramework"";
using (CustomerEntities entities = new CustomerEntities(connectionString))
{
  CustomerEntity entity = new CustomerEntity();
  // do more
  entities.CustomerEntities.Add(entity);
  entities.SaveChanges();
}

When I execute the code above (with the {parameter} values replaced), I get the following error:

当我执行上面的代码(替换了 {parameter} 值)时,出现以下错误:

Keyword not supported: 'data source'.

不支持的关键字:“数据源”。

What am I doing wrong?

我究竟做错了什么?

采纳答案by scartag

Change this.

改变这个。

string connectionString = "metadata=res://*/DataModels.CustomerDataModel.csdl|res://*/DataModels.CustomerDataModel.ssdl|res://*/DataModels.CustomerDataModel.msl;provider=System.Data.SqlClient;provider connection string="data source=tcp:{serverName},{portNumber};initial catalog={databaseName};user id={username};multipleactiveresultsets=True;application name=EntityFramework"";

To this (note how i escaped the " character as "" )

为此(请注意我如何将 " 字符转义为 "" )

string connectionString = @"metadata=res://*/DataModels.CustomerDataModel.csdl|res://*/DataModels.CustomerDataModel.ssdl|res://*/DataModels.CustomerDataModel.msl;provider=System.Data.SqlClient;provider connection string= ""data source=tcp:{serverName},{portNumber};initial catalog={databaseName};user id={username};multipleactiveresultsets=True;application name=EntityFramework""";

回答by Mukus

I know this was asked 10 months ago, but I found an easier way to specify the connectionString:

我知道这是 10 个月前问过的,但我找到了一种更简单的方法来指定 connectionString:

If your config file has it as:

如果你的配置文件是这样的:

<connectionStrings>
<add name="CustomerDataModel" connectionString="metadata=res://*/EntityFramework.CustomerDataModel.csdl|res://*/EntityFramework.CustomerDataModel.ssdl|res://*/EntityFramework.CustomerDataModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\CustomerDataModel;initial catalog=CustomerDB;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

You can specify it as -

您可以将其指定为 -

public const string ConnectionString = @"name=CustomerDataModel";
..
CustomerDBContext context = new CustomerDBContext(ConnectionString );

No need to worry about quotes. Lot cleaner.

无需担心报价。更清洁。

回答by Bedouin

It is easier to use EntityConnectionStringBuilderand SqlConnectionStringBuilderto change parameters as you want.

使用EntityConnectionStringBuilderSqlConnectionStringBuilder根据需要更改参数更容易。

回答by Vijay Sutaria

set multiple connection strings in your web.config, and follow below:

在您的 web.config 中设置多个连接字符串,然后按照以下步骤操作:

public partial class MyDatabaseEntities
{
    public MyDatabaseEntities(string connection)
        : base(connection)
    {
    }
}

and then wherever you want to create instance of entities, pass connection string name in parameter:

然后在任何要创建实体实例的地方,在参数中传递连接字符串名称:

MyDatabaseEntities entities = new MyDatabaseEntities("CONNECTION_STRING_NAME");

I hope this will help.

我希望这将有所帮助。

Thanks

谢谢