繁体   English   中英

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

[英]Why won't connection string work for EF migration?

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

这里有更多细节:

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 Gallery MVC站点中的连接字符串:

    <add name="Gallery.SqlServer" connectionString="Server=(localdb)\v11.0;Database=NuGetGallery11;User ID=testuser;Password=notmyrealpassword;Connect Timeout=60" providerName="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();

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

我的问题是在这等后期解决了这里 您必须在管理器控制台中设置默认项目并设置解决方案的启动项目,以便更新命令找到连接字符串以及Aydin所说的内容,但在这种情况下,该部分已经为我设置。

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

以下是如何从头开始设置实体框架的方法......

  1. 打开具有管理权限的VS (只是谨慎)
  2. 创建一个新的Console application
  3. 打开包管理器控制台
    • 输入:`PM> Install-Package EntityFramework
  4. 打开App.Config文件

    • 添加以下内容

       <connectionStrings> <add name="Gallery.SqlServer" connectionString="Server=(localdb)\\v11.0;Database=GalleryExample;" providerName="System.Data.SqlClient"/> </connectionStrings> 
  5. 创建一个名为Foo的新类

     public class Foo { public Foo() { this.Id = Guid.NewGuid() .ToString(); } public string Id { get; set; } } 
  6. 创建一个名为EfContext的新类

     public class EfContext : DbContext { public EfContext() : base("Gallery.SqlServer") { } public DbSet<Foo> Foos { get; set; } } 
  7. 再次打开包管理器控制台

    • PM>启用 - 迁移
    • PM> Add-Migration InitialMigration
    • PM>更新 - 数据库
  8. 享受你的新数据库......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM