首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么连接字符串不适用于EF迁移?

为什么连接字符串不适用于EF迁移?
EN

Stack Overflow用户
提问于 2015-04-27 20:56:03
回答 2查看 4.3K关注 0票数 3

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

下面是更多的细节:

代码语言:javascript
复制
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)

下面是我在NuGet库MVC站点中的连接字符串:

代码语言:javascript
复制
    <add name="Gallery.SqlServer" connectionString="Server=(localdb)\v11.0;Database=NuGetGallery11;User ID=testuser;Password=notmyrealpassword;Connect Timeout=60" providerName="System.Data.SqlClient"/>

这是我的测试程序。两个应用程序中的连接字符串都是相同的。

代码语言:javascript
复制
            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();

任何帮助都将不胜感激。谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-04-28 18:00:06

我的问题在另一个帖子here中解决了。您必须在manager控制台中设置默认项目,并为解决方案设置启动项目,以便update命令查找连接字符串以及Aydin所说的内容,但在本例中,该部分已经为我设置好了。

票数 3
EN

Stack Overflow用户

发布于 2015-04-27 21:25:45

您的问题是EntityFramework不知道您的连接字符串名为Gallery.SqlServer.

下面是如何从零开始建立实体框架..。

  1. 开放与行政权力(只是谨慎)
  2. 创建一个新的Console application
  3. 打开包管理器控制台
    • 输入:`PM> Install-Package EntityFramework

  1. 打开App.Config文件
代码语言:javascript
复制
- Add the following     

  1. 创建一个名为Foo的新类 公共类Foo { public Foo() { this.Id = Guid.NewGuid() .ToString();}公共字符串Id { get;set;}
  2. 创建一个名为EfContext的新类 公共类EfContext : DbContext { public EfContext():base("Gallery.SqlServer") {} public DbSet Foos { get;set;}
  3. 再次打开包管理器控制台
代码语言:javascript
复制
- PM> Enable-Migrations
- PM> Add-Migration InitialMigration
- PM> Update-Database

  1. 好好享受你的新分贝..。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29905785

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档