这段代码工作正常:
<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实例的远程服务器上运行,并且在加载页面时,将显示以下消息:
An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct.
解决方法
问题在于Integrated Security参数.当它设置为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>