【问题标题】:Why won't connection string work for EF migration?为什么连接字符串不适用于 EF 迁移?
【发布时间】:2015-04-27 20:56:03
【问题描述】:

我创建了一个用于 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();

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

【问题讨论】:

  • 在通过 EF 连接到数据库时,您需要在 Google 上搜索如何格式化连接字符串,这是一个链接,您可以查看以查找工作示例 C# connection strings
  • 从 DbContext 派生的类在哪里?

标签: c# sql-server entity-framework database-connection


【解决方案1】:

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

【讨论】:

  • “并设置解决方案的启动项目,以便更新命令找到连接字符串”这解决了我的问题。谢谢你。谢谢你。谢谢!
【解决方案2】:

您的问题是 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> 添加-迁移 InitialMigration
    • PM> 更新数据库
  8. 享受您的新数据库....

【讨论】:

  • 艾丁,感谢您的帮助。这适用于创建一个简单的示例,但我仍然对为什么我的不起作用感到困惑。需要明确的是,这是来自其 github 存储库的 NuGet Gallery 的来源,它应该可以完全正常工作。它已经有一个将连接名称作为迁移参数的构造函数: public EntitiesContext() : base("Gallery.SqlServer") // 在 web.config 中使用连接字符串(如果找到){ }跨度>
  • 您的凭据实际上可能是罪魁祸首...据我所知,LocalDB 使用 Windows 身份验证,如果我的示例有效但另一个无效,那么它一定是您的连接字符串,而我的连接字符串和你的唯一区别是你的尝试交出用户名并通过
  • 我想通了!看起来除了在包管理器控制台中将默认项目设置为 NuGetGallery 之外,您还必须将 NuGetGallery 设置为解决方案的启动项目,以便它从正确的 web.config 文件中读取。必须两次指定该项目才能找到 web.config 和连接字符串,这似乎有点烦人和多余,但我想这对你来说是微软。
  • hahahahaha,我很惊讶我忽略了这一点,因为我通常将实体框架和域模型保存在与启动项目不同的库中,并在通过 npm 应用迁移时明确定位它们
猜你喜欢
  • 2020-10-25
  • 2021-10-08
  • 2018-06-13
  • 1970-01-01
  • 1970-01-01
  • 2019-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多