为什么连接字符串不能用于EF迁移

为什么连接字符串不能用于EF迁移

本文介绍了为什么连接字符串不能用于EF迁移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个数据库,用于NuGet Gallery实现。我可以在sql manager 2012中看到数据库,我可以使用我的连接字符串从我写的测试程序中访问它。但是,当我尝试在软件包管理器控制台中运行Update-Database命令时,要使EF设置数据库,我会收到错误:服务器未找到或无法访问。



这里有更多的细节:

  PM>更新数据库-Verbose 

使用StartUp项目'NuGetGallery.Operations'。
使用NuGet项目'NuGetGallery'。
指定'-Verbose'标志来查看应用于目标数据库的SQL语句。

System.Data.ProviderIncompatibleException:
从数据库获取提供者信息时发生错误。
这可能是由实体框架使用不正确的连接字符串引起的。
检查内部异常的详细信息,并确保连接字符串正确。 --->

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 =(localdb) \v11.0; Database = NuGetGallery11; User ID = testuser; Password = notmyrealpassword; Connect Timeout = 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},Name:{1},Email:{2},id,name,email);

}
}
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迁移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 16:18