English 中文(简体)
实体框架 Web 配置文件
原标题:entity framework web config file

这部守则很管用:

<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 实例的远程服务器上运行。 当页面加载时, 显示以下信息 :

从数据库获取提供者信息时出错。 这可能是由实体框架使用错误的连接字符串造成的。 请检查内部的例外细节, 并确保连接字符串正确 。

问题回答

问题在于集成安全参数。 当它被设置为 True 时, 如果您指定了用户和密码, 则. Net 尝试打开当前用户事件的连接。 所以要打开与特定用户的连接, 将集成安全设置为假, 它将有效 。

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

在这里我必须做的是使用动态连接字符串设置使 EF 连接, 尽管我的设置是用户输入, 而不是网络配置 :

        // 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;
        }

确保代码连接字符串正确 :

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

检查您的 sql 服务器 。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签