简体   繁体   English

无法生成显式迁移,因为以下显式迁移待处理

[英]Unable to generate an explicit migration because the following explicit migrations are pending

I'm using EF 6.1 and I enabled code first migration in my project by我正在使用EF 6.1并通过以下方式在我的项目中启用了代码优先迁移

Enable-Migrations
Add-Migration InitializeDb -ConnectionProviderName System.Data.SqlClient -ConnectionString "Data Source=myServer;Initial Catalog=myDb;Persist Security Info=True;User ID=sa;password=******;application name = L4"
Update-Database  -ConnectionProviderName System.Data.SqlClient -ConnectionString "Data Source=myServer;Initial Catalog=myDb;Persist Security Info=True;User ID=sa;password=******;application name = L4" -verbose -script

When I specify my ConnectionString , ConnectionProviderName explicitly with Add-Migration and Update-database in package manager console it work correctly:当我在包管理器控制台中使用Add-MigrationUpdate-database明确指定我的ConnectionStringConnectionProviderName ,它可以正常工作:

Add-migration updateXtable -ConnectionProviderName System.Data.SqlClient -ConnectionString "Data Source=myServer;Initial Catalog=myDb;Persist Security Info=True;User ID=sa;password=******;application name = L4"

but when I use Add-Migration without extra informations:但是当我在没有额外信息的情况下使用Add-Migration

add-migration updateXtable

I get following error:我收到以下错误:

Unable to generate an explicit migration because the following explicit migrations are pending: [201408300955376_InitializeDb, 201408311028404_Test].无法生成显式迁移,因为以下显式迁移未决:[201408300955376_InitializeDb, 201408311028404_Test]。 Apply the pending explicit migrations before attempting to generate a new explicit migration.在尝试生成新的显式迁移之前应用挂起的显式迁移。

So, I have to use following command each time I need update my Database:因此,每次需要更新数据库时,我都必须使用以下命令:

Add-Migration UpdateXTable -ConnectionProviderName System.Data.SqlClient -ConnectionString "Data Source=myServer;Initial Catalog=myDb;Persist Security Info=True;User ID=sa;password=******;application name = L4"
Update-Database  -ConnectionProviderName System.Data.SqlClient -ConnectionString "Data Source=myServer;Initial Catalog=myDb;Persist Security Info=True;User ID=sa;password=******;application name = L4" -verbose -script

It's my project(that contains my DbContext ) app.config file:这是我的项目(包含我的DbContextapp.config文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
 <entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory,  EntityFramework" />
 <providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
 </providers>
 </entityFramework>
 <connectionStrings>
 <add name="ERPContext" connectionString="Data Source=myServer;Initial Catalog=myDb;Persist Security Info=True;User ID=sa;password=******;application name = L4" providerName="System.Data.SqlClient" />
 </connectionStrings>

 <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
 </startup>   
</configuration>

Does anyone know where is the problem?有谁知道问题出在哪里?

To resolve this issue, I changed my default project to the one with the app.config containing the proper connection string.为了解决这个问题,我将我的默认项目更改为app.config包含正确连接字符串的项目。

This should have been obvious, since the system was detecting none of the migrations as applied - an obvious sign that it was not finding the DB, an obvious reason for that being that it could not find the connection string (as OP concluded).这应该是显而易见的,因为系统没有检测到任何已应用的迁移 - 一个明显的迹象表明它没有找到数据库,一个明显的原因是它找不到连接字符串(如 OP 得出的​​结论)。

"Hindsight is 20/20". “事后诸葛亮是 20/20”。

I was also getting this error.我也收到了这个错误。 In addition to correctly setting the Default Project (as mentioned by ANeves), I also had to set the project with the connection string as the StartUp Project in Solution Explorer.除了正确设置默认项目(如 ANeves 所述)之外,我还必须将带有连接字符串的项目设置为解决方案资源管理器中的启动项目。 Only once I correctly set both of these did the error go away.只有在我正确设置了这两个之后,错误才会消失。

Finally I found the problem!终于我发现了问题! As Mohamad Bataineh said in this thread (see the answers)正如Mohamad Bataineh这个帖子中所说的(见答案)

In your DbContext class, you have need to specify the base class constructer with the proper name to your sql source.在您的 DbContext 类中,您需要为您的 sql 源指定具有正确名称的基类构造函数。 For example name=MyDBEntities例如 name=MyDBEntities

In the other word I changed my existing dbcontext's constructor换句话说,我改变了我现有的 dbcontext 的构造函数

public MyDbContext()
{
} 

To

public MyDbContext(): base("name=ERPContext")
{
} 

通过删除我的 Migrations 文件夹(在解决方案资源管理器中)中的相应迁移,然后添加新迁移并最终更新数据库,我能够摆脱该消息。

Just delete that migration from migration folder which showing error and not updating database.只需从显示错误而不更新数据库的迁移文件夹中删除该迁移。 Then again add migration.然后再次添加迁移。 It will work.它会起作用。

在我的情况下,我的工作站的IP地址没有添加到SQL Server的防火墙例外。

只需删除您有错误的迁移,它对我有用

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

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