简体   繁体   中英

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

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-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 updateXtable

I get following error:

Unable to generate an explicit migration because the following explicit migrations are pending: [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:

<?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.

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).

"Hindsight is 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. 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)

In your DbContext class, you have need to specify the base class constructer with the proper name to your sql source. For example name=MyDBEntities

In the other word I changed my existing dbcontext's constructor

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的防火墙例外。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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