Ian
Ian

Reputation: 34489

Forceful reset of Entity Framework Migrations

I had an issue where some of my migrations weren't quite working and I didn't seem to be able to fix it. Therefore I decided to start again. I did the following:

I then re-installed EF5 and ran the following;

Enable-Migrations –EnableAutomaticMigrations -Force

This creates my migration.cs file but doesn't touch the database. If I run some code that tries to use the context it complains that the tables don't exist. So I then created a migration:

Add-Migration InitialCreate

The migration this creates however has remembered some state, ignoring the fact it needs to create tables and just lists:

 public partial class InitialCreate : DbMigration
{
    public override void Up()
    {
        AddColumn("dbo.NetC_EF_ShippingRate", "CurrencyName", c => c.String());
        AddColumn("dbo.NetC_EF_ShippingRate", "CreationDate", c => c.DateTime(nullable: false));
    }

    public override void Down()
    {
        DropColumn("dbo.NetC_EF_ShippingRate", "CreationDate");
        DropColumn("dbo.NetC_EF_ShippingRate", "CurrencyName");
    }
}

How can I get it to forget the state fully and create all tables etc related to the context as if I was starting from scratch?

EDIT I should probably add if I simply try and run this manually created migration I just get errors:

Cannot find the object "dbo.NetC_EF_ShippingRate" because it does not exist or you do not have permissions.

App.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

  <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <!--<providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>-->
  </entityFramework>
  <connectionStrings>
    <add name="FreightContext" providerName="System.Data.SqlClient" connectionString="Persist Security Info=False;database=DS_Kentico;server=FS-01\DEVSQL2008R2;user id=****;password=****;Current Language=English;Connection Timeout=240;" />
  </connectionStrings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
  </startup>
</configuration>

web.config

<connectionStrings>
    <clear />
    <add name="CMSConnectionString" connectionString="Persist Security Info=False;database=DeltaShelving_Kentico;server=FS-01\DEVSQL2008R2;user id=generic_webuser;password=generic_webuser;Current Language=English;Connection Timeout=240;" />
    <add name="FreightContext" providerName="System.Data.SqlClient" connectionString="Persist Security Info=False;database=DS_Kentico;server=FS-01\DEVSQL2008R2;user id=****;password=****;Current Language=English;Connection Timeout=240;" />
  </connectionStrings>

Upvotes: 0

Views: 893

Answers (1)

Richard
Richard

Reputation: 1692

Entity Framework will be looking for a valid connection string in the app.config of the target project. In this case it can't find it.

The target project for EF Migrations is specified either by the -ProjectName option on the Add-Migration command, or it reverts to the default project as defined in the Package Manager Console.

Try setting the project name explicitly to the project that contains your DbContext.

Upvotes: 2

Related Questions