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

c# ef-migrations entity-framework

Question

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?

1
11
9/3/2014 8:41:25 AM

Accepted Answer

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")
{
} 
3
5/23/2017 11:54:44 AM

Popular Answer

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



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow