Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 655
Masoud Avatar asked Sep 03 '14 07:09

Masoud


1 Answers

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

like image 139
ANeves Avatar answered Oct 13 '22 00:10

ANeves