简体   繁体   English

代码首次迁移中更新数据库命令出错

[英]Error in update-database command in code first migration

I am working on Desktop application in WPF and creating SqlRepository with LocalDB to store data.我正在 WPF 中开发桌面应用程序,并使用LocalDB创建 SqlRepository 来存储数据。 I am using following tools我正在使用以下工具

  • Visual Studio 2013 Community with update 2带有更新 2 的 Visual Studio 2013 社区
  • Entity Framework 6.1.2 for Code first migration用于代码优先迁移的实体框架 6.1.2

I have created local database with Microsoft SQL Server Database File (SqlClient) configuration.我已经使用 Microsoft SQL Server 数据库文件 (SqlClient) 配置创建了本地数据库。 Database created successfully and below is connection string fetched from Sql数据库创建成功,下面是从Sql中获取的连接字符串

Data Source=(LocalDB)\v11.0;Initial Catalog=D:\USERS\USERNAME\DOCUMENTS\TestDatabase\testdb.MDF;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False

I am using same connection string in app.config.我在 app.config 中使用相同的连接字符串。 I can view database object in SQL Server Database explorer in Visual Studio.我可以在 Visual Studio 的 SQL Server 数据库资源管理器中查看数据库对象。 But when run update-database command in Nuget package manager console, I am getting below error:但是在 Nuget 包管理器控制台中运行 update-database 命令时,出现以下错误:

A network-related or instance-specific error occurred while establishing a connection to SQL Server.与 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.验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) (提供者:SQL 网络接口,错误:26 - 错误定位服务器/指定的实例)

I had similar problem and fixed it when I changed the "start-up project" from another module to the module containing references to all other projects in the solution.当我将“启动项目”从另一个模块更改为包含对解决方案中所有其他项目的引用的模块时,我遇到了类似的问题并修复了它。 Right-click module >> click "Set as StartUp Project"右键单击模块>>单击“设置为启动项目”

Diego's answer is correct.迭戈的回答是正确的。

This problem occurs when there is no connection string in project marked as startup project.当标记为启动项目的项目中没有连接字符串时,会出现此问题。 Then EF tries to connect to some default database engine to perform update.然后 EF 尝试连接到某个默认数据库引擎以执行更新。 In my case it tried to use express, and for some reason it couldn't connect.就我而言,它尝试使用 express,但由于某种原因无法连接。 And the error was thrown.并且错误被抛出。

Run your "update-database" with option "-Verbose".使用选项“-Verbose”运行“更新数据库”。 One of the lines there shows which StartUp project is used.其中一行显示使用了哪个 StartUp 项目。 Check your connection string in this project, or change the startup project to the one that has correct connection string.检查此项目中的连接字符串,或将启动项目更改为具有正确连接字符串的项目。 That solves the problem.这解决了问题。

As explained in other answers, the problem usually comes from having the wrong Initial project in Package Manager Console.正如其他答案中所述,问题通常来自于包管理器控制台中的初始项目错误。 In my case the console was ignoring the value I selected in the Default Project drop down list, and also the -StartUpProjectName parameter, and reproducing the wrong behavior of trying to connect to some default database engine, as Mikk's answer describes, in my case using a SqlExpress engine.在我的情况下,控制台忽略了我在 Default Project 下拉列表中选择的值以及-StartUpProjectName参数,并重现了尝试连接到某些默认数据库引擎的错误行为,如 Mikk 的回答所述,在我的情况下使用一个 SqlExpress 引擎。

My problem was caused by a wrong solution configuration: if your solution has several projects and is meant to be run with the configuration option "Multiple startup projects", but you just downloaded it from your source code control repository, then it is possible that the default configuration option "Single startup project" is being applied to the solution (this config value usually is not checked-in in the source code control).我的问题是由错误的解决方案配置引起的:如果您的解决方案有多个项目并且打算使用配置选项“多个启动项目”运行,但您只是从源代码控制存储库下载它,那么可能是默认配置选项“单个启动项目”正在应用于解决方案(此配置值通常不会在源代码控制中签入)。 In this case the Package Manager Console just ignores the startup project selected in its combo and just applies the default startup project in the solution, which may not have a connection string, as specified in Mikk's answer.在这种情况下,包管理器控制台只会忽略在其组合中选择的启动项目,而只会应用解决方案中的默认启动项目,该项目可能没有连接字符串,如 Mikk 的回答中所指定。

So I fixed it by changing the solution properties: Common properties / Startup project / Select Multiple startup projects instead of Single startup project , and after that the Package Manager Console would accept the Project name and update the right database.所以我通过更改解决方案属性来修复它: Common properties / Startup project / Select Multiple startup projects而不是Single startup project ,然后包管理器控制台将接受项目名称并更新正确的数据库。

我能够通过向 update-database 命令添加参数来解决这个问题:

update-database -StartupProjectName MyApp.Web -ConnectionStringName MyAppConnectStringInWebConfig

I solved this error changing the initial project to the Entity Project.我解决了这个错误,将初始项目更改为实体项目。

Look:看:

Update-Database -Verbose Using StartUp project 'SCVE.Web'. Update-Database -Verbose使用启动项目“SCVE.Web”。 <-- this ir error Using NuGet project 'SCVE.EntityFramework'. <-- 这个错误使用 NuGet 项目“SCVE.EntityFramework”。

A network-related or instance-specific error occurred while establishing a connection to SQL Server.与 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.验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) (提供者:SQL 网络接口,错误:26 - 错误定位服务器/指定的实例)

This error message informs you that it is not possible to connect to SQL Server .此错误消息通知您无法连接到SQL Server The possible reasons and their elimination is described below:可能的原因及其排除方法描述如下:

1) SQL Server is not started. 1) SQL Server 未启动。 Starting of it will allow you to see your SQL Server/instance in the drop-down list of available SQL Servers.启动它将允许您在可用 SQL Server 的下拉列表中查看您的 SQL Server/实例。

  • Go to the Start menu -> Control Panel -> Administration Tools -> Services.转到开始菜单 -> 控制面板 -> 管理工具 -> 服务。
  • In the list of services find SQL Server (instance name, by default it is EZPARTS5) and check its status, it must be Started在服务列表中找到SQL Server(实例名,默认为EZPARTS5)并查看其状态,必须是Started
    (if it is not started, then right click on SQL Server and select (如果没有启动就在SQL Server上右击选择
    Start from the context menu).从上下文菜单开始)。

2) Firewall is blocking port 1433 (MSSQL standard port for connections). 2) 防火墙阻止了端口 1433(用于连接的 MSSQL 标准端口)。 It can be disabled following the steps below:可以按照以下步骤禁用它:

  • Go to the Start menu -> Control Panel -> Administration Tools -> Services.转到开始菜单 -> 控制面板 -> 管理工具 -> 服务。
  • Find Firewall service, it must be disabled (if it is not, then right click the service and select Stop from the context menu).找到防火墙服务,它必须被禁用(如果不是,则右键单击该服务并从上下文菜单中选择停止)。

Note: More information on this can be found on the official Microsoft site: Link注意:更多信息可以在微软官方网站上找到: 链接

3) TCP/IP protocol is disabled for MSSQL protocols. 3) MSSQL 协议禁用 TCP/IP 协议。 To enable it, see the steps below:要启用它,请参阅以下步骤:

  • Navigate to SQL Server Configuration Manager in the Start menu.在“开始”菜单中导航到 SQL Server 配置管理器。
  • Specify settings for TCP/IP protocol in SQL Server Configuration Manager.在 SQL Server 配置管理器中指定 TCP/IP 协议的设置。
  • Restart the computer.重新启动计算机。

Note: More information on this can be found on the official Microsoft site: Link注意:更多信息可以在微软官方网站上找到: 链接

4) Make sure your database engine is configured to accept remote connections (If you are using centralized database): 4) 确保您的数据库引擎配置为接受远程连接(如果您使用的是集中式数据库):

  • Open SQL Server Management Studio.打开 SQL Server 管理工作室。
  • Right click SQL Server instance -> Properties -> Connections -> Check the Allow remote connections to this server box.右键单击 SQL Server 实例 -> 属性 -> 连接 -> 选中允许远程连接到此服务器框。
  • Go to the General section and check name of SQL Server specified in the Name field.转到“常规”部分并检查“名称”字段中指定的 SQL Server 的名称。

5) If you are using a named SQL Server instance, make sure you are using that instance name in your connection strings. 5) 如果您使用的是命名的 SQL Server 实例,请确保在连接字符串中使用该实例名称。 Usually the format needed to specify the database server is machinename\\instancename.通常指定数据库服务器所需的格式是machinename\\instancename。

6) Make sure your login account has access permission on the database you used during login. 6) 确保您的登录帐户对您登录时使用的数据库具有访问权限。 Alternative: If you still can't get any connection, you may want to create a SQL account on the server, a corresponding SQL user on the database in question, and just use this username/password login data to connect to SQL Server.替代方案:如果仍然无法连接,您可能需要在服务器上创建一个 SQL 帐户,在有问题的数据库上创建一个对应的 SQL 用户,然后使用此用户名/密码登录数据连接到 SQL Server。

Referred from this link来自此链接

Try with this Connectionstring:尝试使用此连接字符串:

<connectionStrings>
  <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=D:\USERS\USERNAME\DOCUMENTS\TestDatabase\testdb.MDF;Initial Catalog=testdb;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

If you are trying to connect to Sql Server instead of localdb then make sure the default connection factory in web.config file is as below:如果您尝试连接到 Sql Server 而不是 localdb,请确保 web.config 文件中的默认连接工厂如下:

<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="Data Source=.\SQLEXPRESS2012; Integrated Security=True; MultipleActiveResultSets=True" />
  </parameters>
 </defaultConnectionFactory>

I had this same problem with a freshly created ASP.NET project.我在新创建的 ASP.NET 项目中遇到了同样的问题。

First I tried to set the startup project as mentioned above.首先,我尝试设置启动项目,如上所述。 There was no startup project selected and I was unable to select one.没有选择启动项目,我无法选择一个。

Ultimately I updated my Visual Studio from 2015 Update 1 to 2015 Update 3 and最终我将 Visual Studio 从 2015 Update 1 更新到 2015 Update 3 和

update-database

executed without problems.执行没有问题。

PM>Update-Database

error and solution which I did today in 4 hours.我今天在 4 小时内完成的错误和解决方案。 I am very much new in .net coding, but yes in PHP I have 10+ years experience.我对 .net 编码非常陌生,但是在 PHP 方面我有 10 多年的经验。 It took me 4 hours to solve this.我花了4个小时来解决这个问题。 1.My first Mistake: I was not having Microsoft SQL Server 2019 in my laptop locally I was having this through AZURE which is not works for localhost, so I installed this locally after download, it took around 2 Hours. 1.我的first Mistake:我的笔记本电脑本地没有安装 Microsoft SQL Server 2019 我是通过 AZURE 安装的,这不适用于本地主机,所以下载后我在本地安装了它,大约需要 2 小时。 2.Second changed mistake= 2.第二次改错=

 "DevConnection": "Server=localhost;Database=PaymentDetailDB;Trusted_Connection=True;MultipleActiveResultSets=True;"

then it works for me那么它对我有用

错误

错误解决方法

错误已解决 错误解决后的数据库

暂无
暂无

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

相关问题 Update-Database命令在建立与服务器的连接时出错(代码优先迁移) - Update-Database command giving error in establishing connection to the server (Code First Migration) 运行代码第一次迁移update-database时出错 - Error running code first migration update-database 尝试首先更新数据库代码时出错 - Error on trying to Update-Database code first 实体框架代码优先迁移因更新数据库而失败 - Entity Framework code-first migration fails with update-database 为什么在这种情况下为Update-Database破坏了生成的Code First迁移? - Why is the generated Code First Migration broken for Update-Database in this instance? 更新数据库代码首先迁移 ASP.NET MVC 时,ORA-00955 名称已被现有对象错误 - ORA-00955 name is already used by an existing object Error when Update-Database Code First Migration ASP.NET MVC 实体框架代码优先:迁移失败,更新数据库,强制不必要的(?)添加迁移 - Entity Framework code-first: migration fails with update-database, forces unneccessary(?) add-migration “更新数据库”命令不会更新到最新版本的迁移 - "Update-Database" command does not update to the latest version of Migration 初始迁移后,EF6中的update-database命令不起作用 - update-database command in EF6 not working after initial migration 使用Entity Framework,Code First(POCO)和Code Contracts运行Update-Database时出错 - Error When running Update-Database using Entity Framework, Code First (POCO) and Code Contracts
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM