【问题标题】:Set Entity Framework Connection String at Runtime in C#在 C# 运行时设置实体框架连接字符串
【发布时间】:2013-02-03 14:31:47
【问题描述】:

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

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();
}

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

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

我做错了什么?

【问题讨论】:

    标签: c# entity-framework


    【解决方案1】:

    改变这个。

    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"";
    

    对此(注意我是如何将 " 字符转义为 "" 的)

    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""";
    

    【讨论】:

      【解决方案2】:

      我知道这是 10 个月前提出的,但我找到了一种更简单的方法来指定连接字符串:

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

      <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" />
      

      您可以将其指定为 -

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

      无需担心报价。更清洁。

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

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

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

          然后无论你想在哪里创建实体实例,在参数中传递连接字符串名称:

          MyDatabaseEntities entities = new MyDatabaseEntities("CONNECTION_STRING_NAME");
          

          我希望这会有所帮助。

          谢谢

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-07-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-12-12
            • 1970-01-01
            • 1970-01-01
            • 2012-10-08
            相关资源
            最近更新 更多