9

此代码工作正常:

<connectionStrings>
    <add name="EFDbContext" connectionString="Data Source=.\SQLEXPRESS; Initial Catalog=myDB;Integrated Security=SSPI;
 " providerName="System.Data.SqlClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="Data Source=.\SQLEXPRESS; Integrated Security=True; MultipleActiveResultSets=True" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>

但这段代码不起作用:

<connectionStrings>
    <add name="EFDbContext" connectionString="Data Source=.\MSSQLSERVER2008; Initial Catalog=myDb;Integrated Security=SSPI;User ID=useradmin; Password=pass; " providerName="System.Data.SqlClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="Data Source=.\MSSQLSERVER2008; Integrated Security=True; MultipleActiveResultSets=True" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>

第二个代码必须在具有 MSQSERVER2008 实例的远程服务器上运行,并且在加载页面时,会出现以下消息:

从数据库获取提供者信息时出错。这可能是由 Entity Framework 使用不正确的连接字符串引起的。检查内部异常以获取详细信息并确保连接字符串正确。

4

3 回答 3

12

问题在于集成安全参数。当它设置为 True 时,如果您指定用户和密码,.Net 会尝试打开与当前用户事件的连接。因此,要打开与特定用户的连接,请将 Integrated Security 设置为 False,它将起作用。

<connectionStrings>
    <add name="EFDbContext" connectionString="Data Source=.\MSSQLSERVER2008; Initial Catalog=myDb;Integrated Security=SSPI;User ID=useradmin; Password=pass; " providerName="System.Data.SqlClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="Data Source=.\MSSQLSERVER2008; Integrated Security=False; MultipleActiveResultSets=True" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
于 2012-05-23T12:19:25.053 回答
2

这是我必须使用动态连接字符串设置进行 EF 连接的操作,尽管我的设置是用户输入,而不是 Web 配置:

        // Set the properties for the data source.
        sqlBuilder.ConnectionString = "Integrated Security=SSPI;";
        sqlBuilder.DataSource = serverName;
        sqlBuilder.InitialCatalog = databaseName;
        sqlBuilder.ConnectTimeout = 60;
        sqlBuilder.MultipleActiveResultSets = true;
        if(!string.IsNullOrEmpty(userName) || !string.IsNullOrEmpty(password))
        {
            sqlBuilder.UserID = userName;
            sqlBuilder.Password = password;
            //sqlBuilder.IntegratedSecurity = false;
        }
        else
        {
            sqlBuilder.IntegratedSecurity = true;
        }
于 2012-10-10T19:48:36.317 回答
0

确保在您的代码连接字符串是正确的:

"Data Source=.\MSSQLSERVER2008; Initial Catalog=myDb;Integrated Security=SSPI;User ID=useradmin; Password=pass;" 

检查你的 sql 服务器。

于 2013-01-23T06:24:26.757 回答