问题描述
我创建了一个用于NuGet Gallery实现的数据库。我可以看到数据库在sql manager 2012,我可以从我写的测试程序访问它,使用我的连接字符串。但是,当我尝试在包管理器控制台中运行Update-Database命令以使EF设置数据库时,我一直收到错误:找不到服务器或无法访问服务器。
这里有更多细节:
PM> Update-Database -Verbose
使用StartUp项目'NuGetGallery.Operations'。
使用NuGet项目NuGetGallery。
指定-Verbose标志以查看应用于目标数据库的SQL语句。
System.Data.ProviderIncompatibleException:
从数据库获取提供程序信息时出错。
这可能是由Entity Framework使用不正确的连接字符串引起的。
检查内部异常以获取详细信息,并确保连接字符串正确。 --->
System.Data.ProviderIncompatibleException:提供程序未返回ProviderManifestToken字符串。 --->
System.Data.SqlClient.SqlException:与SQL Server建立连接时发生与网络相关或特定于实例的错误。找不到服务器或无法访问。验证实例名称是否正确,并且SQL Server配置为允许远程连接。 (提供程序:SQL网络接口,错误:26
- 错误定位服务器/实例指定)在System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,布尔breakConnection,Action`1 wrapCloseInAction)在系统.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,Boolean callerHasConnectionLock,Boolean asyncClose)
NuGet Gallery MVC网站中的字符串:
< add name =Gallery.SqlServerconnectionString =Server = \v11.0; Database = NuGetGallery11;用户ID = testuser;密码= notmyrealpassword;连接超时= 60providerName =System.Data.SqlClient/>
这里是我的测试程序。这两个应用程序中的连接字符串是相同的。
var sqlConnection2 = new SqlConnection(ConfigurationManager.ConnectionStrings [Gallery.SqlServer]。 ConnectionString);
sqlConnection2.Open();
using(sqlConnection2){
var sqlCmd = new SqlCommand(select * from Foo,sqlConnection2);
var foo = sqlCmd.ExecuteReader();
while(foo.Read()){
int id = foo.GetInt32(0);
var name = foo.GetString(1);
var email = foo.GetString(2);
Console.WriteLine(ID:{0},名称:{1},电子邮件:{2},id,姓名,电子邮件);
}
}
Console.Read();
任何帮助将非常感谢。感谢!
我的问题在这个其他帖子中解决了。您必须在管理器控制台中设置默认项目,并为解决方案设置启动项目,以便update命令可以找到连接字符串以及Aydin说,但在这种情况下,该部分已经为我设置。 / p>
I have created a database for use with a NuGet Gallery implementation. I can see the database in sql manager 2012 and I can access it from a test program I wrote, using my connection string. However, when I try to run the Update-Database command in the package manager console, to have EF set up the database, I keep getting the error: "The server was not found or was not accessible.".
Here's more detail:
PM> Update-Database -Verbose
Using StartUp project 'NuGetGallery.Operations'.
Using NuGet project 'NuGetGallery'.
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
System.Data.ProviderIncompatibleException:
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. --->
System.Data.ProviderIncompatibleException: The provider did not return a ProviderManifestToken string. --->
System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26
- Error Locating Server/Instance Specified) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
Here's my connection string in the NuGet Gallery MVC site:
<add name="Gallery.SqlServer" connectionString="Server=(localdb)\v11.0;Database=NuGetGallery11;User ID=testuser;Password=notmyrealpassword;Connect Timeout=60" providerName="System.Data.SqlClient"/>
And here's my test program. The connection strings are identical in both applications.
var sqlConnection2 = new SqlConnection(ConfigurationManager.ConnectionStrings["Gallery.SqlServer"].ConnectionString);
sqlConnection2.Open();
using (sqlConnection2) {
var sqlCmd = new SqlCommand("select * from Foo", sqlConnection2);
var foo = sqlCmd.ExecuteReader();
while (foo.Read()) {
int id = foo.GetInt32(0);
var name = foo.GetString(1);
var email = foo.GetString(2);
Console.WriteLine("ID:{0}, Name:{1}, Email:{2}", id, name, email);
}
}
Console.Read();
Any help would be greatly appreciated. Thanks!
My problem was solved in this other post here. You have to set the default project in the manager console AND set the startup project for the solution in order for the update command to find the connection string and also what Aydin said, but in this case that part was already set up for me.
这篇关于为什么连接字符串不能用于EF迁移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!