EF - 代码优先迁移:处理请求时数据库操作失败

时间:2017-10-30 05:56:40

标签: c# entity-framework-core ef-migrations asp.net-core-1.1

我将ASP.NET MVC Core 1.1.1项目与其SQL Server 2012 db一起发布到了生产中。几天后,我将数据库从生产服务器备份并恢复到开发计算机,并将其重新连接到我的VS2017开发项目。但是当我尝试运行应用程序时,它会给我以下错误:

A database operation failed while processing the request.

InvalidOperationException: An exception has been raised that is likely due to a transient failure. If you are connecting to a SQL Azure database consider using SqlAzureExecutionStrategy. 
SqlException: A connection was successfully established with the server, but then an error occurred during the login process. (provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe.) 
Win32Exception: No process is on the other end of the pipe 

我通过连接到SSMS的同一登录来检查相同的db登录,并且它成功连接到数据库。

此外,在上面的错误消息下面,有以下消息,其中包含项目所有迁移的列表:

Applying existing migrations for MyProjectContext may resolve this issue
There are migrations for MyProjectContext that have not been applied to the database

20161209232754_myFirstMigration
20170121191004_mySecondMigration
20170303172256_myThirdMigration
20170303172256_myFourthMigration

In Visual Studio, you can use the Package Manager Console to apply pending migrations to the database:

PM> Update-Database
Alternatively, you can apply pending migrations from a command prompt at your project directory:

> dotnet ef database update

注意

  1. 开发机器为Windows 10,生产服务器为Windows Server 2012-R2。这两台机器都有相同版本的SQL Server 2012
  2. 我已验证VS2017项目和Db在本地开发计算机上具有完全相同的迁移。部署到生产后,迁移从未在开发计算机上更改。当我从上面的错误消息中运行推荐的命令PM> Update-Database -context MyProjectContext时,我收到错误:CREATE DATABASE permission denied in database 'master'

1 个答案:

答案 0 :(得分:2)

我过去曾遇到类似的情况,其中CREATE DATABASE permission denied in database 'master'消息是完全省略连接字符串引起的(这听起来像一个愚蠢的事情,但它可能通过错误的配置发生多项目解决方案)。

检查您的配置字符串是否存在,具有您希望它具有的名称,并在运行时对您的应用程序可见。如果您在与主应用程序共享的类库中具有Entity Framework代码,请确保在应用迁移之前选择主应用程序作为启动项目,因此EF知道在哪里查找App.config文件。