Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么赢了';连接字符串是否可以用于EF迁移?_C#_Sql Server_Entity Framework_Database Connection - Fatal编程技术网

C# 为什么赢了';连接字符串是否可以用于EF迁移?

C# 为什么赢了';连接字符串是否可以用于EF迁移?,c#,sql-server,entity-framework,database-connection,C#,Sql Server,Entity Framework,Database Connection,我已经创建了一个用于NuGet Gallery实现的数据库。我可以在sql manager 2012中看到数据库,并且可以使用连接字符串从我编写的测试程序中访问它。但是,当我尝试在PackageManager控制台中运行updatedatabase命令以让EF设置数据库时,我不断收到错误:“找不到服务器或无法访问服务器。” 以下是更多细节: PM> Update-Database -Verbose Using StartUp project 'NuGetGallery.Operation

我已经创建了一个用于NuGet Gallery实现的数据库。我可以在sql manager 2012中看到数据库,并且可以使用连接字符串从我编写的测试程序中访问它。但是,当我尝试在PackageManager控制台中运行updatedatabase命令以让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"/>

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

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

下面是如何从头开始设置实体框架

  • 开放VS与管理权限(只是谨慎)
  • 创建新的
    控制台应用程序
  • 打开包管理器控制台
    • 键入:`PM>InstallPackageEntityFramework
  • 打开
    App.Config
    文件

    • 添加以下内容

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

      public class EfContext : DbContext
      {
          public EfContext()
              : base("Gallery.SqlServer")
          {
      
          }
          public DbSet<Foo> Foos { get; set; }
      }
      
      公共类EfContext:DbContext
      {
      公共环境()
      :base(“Gallery.SqlServer”)
      {
      }
      公共DbSet Foos{get;set;}
      }
      
    • 再次打开包管理器控制台

      • PM>启用迁移
      • PM>添加迁移初始迁移
      • PM>更新数据库
    • 享受你的新db


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

      当通过EF连接到数据库时,您需要在谷歌上搜索如何格式化连接字符串。这里有一个链接,您可以查看该链接,以找到一个工作示例,该类将从DbContext派生?Aydin,感谢您的帮助。这为创建一个简单的示例起到了作用,但我仍然不明白为什么我的示例不起作用。要明确的是,这是NuGet Gallery从其github repo获得的源代码,应该可以完全正常工作。它已经有了一个将连接名作为迁移参数的构造函数:public EntitiesContext():base(“Gallery.SqlServer”)//在web.config中使用连接字符串(如果找到了连接字符串){}您的凭据可能是罪魁祸首。。。据我所知,LocalDB使用Windows auth,如果我的示例有效,而另一个则无效,那么它一定是您的连接字符串,我的连接字符串和您的连接字符串之间的唯一区别是您尝试提交用户名和密码,我发现了这一点!看起来,除了在package manager控制台中将默认项目设置为NuGetGallery外,还必须将NuGetGallery设置为解决方案的启动项目,以便从正确的web.config文件中读取。为了找到web.config和连接字符串,必须两次指定该项目,这似乎有点烦人和多余,但我想这是Microsoft为您准备的,我很惊讶我忽略了这一点,因为我通常将实体框架和域模型与启动项目放在一个单独的库中,并在通过npm应用迁移时明确地将它们作为目标“并为解决方案设置启动项目,以便更新命令找到连接字符串”,这解决了我的问题。非常感谢。非常感谢。非常感谢。
      public class Foo
      {
          public Foo()
          {
              this.Id = Guid.NewGuid()
                            .ToString();
          }
      
          public string Id { get; set; }
      }
      
      public class EfContext : DbContext
      {
          public EfContext()
              : base("Gallery.SqlServer")
          {
      
          }
          public DbSet<Foo> Foos { get; set; }
      }