How to recreate my EF CodeFirst Table?

10,648

Solution 1

In system tables there is a table called

__MigrationHistory

...I deleted it.

In Package manager console ran command "update-database" Problem fixed!

More info:

http://blog.oneunicorn.com/2012/02/27/code-first-migrations-making-__migrationhistory-not-a-system-table/

Solution 2

Daf De Giraf has a better answer here that doesn't wipe out your migration history.

If you worked the correct way to create your migrations by using the command Add-Migration "Name_Of_Migration" then you can do the following to get a clean start (reset, with loss of data, of course):

  1. Update-database -TargetMigration:0
    Normally your DB is empty now since the down methods were executed.

  2. Update-database This will recreate your DB to your current migration

Share:
10,648
Pinch
Author by

Pinch

I'm a C# Developer in the NYC area. I just started blogging about parenting and coding. I'd appreciate a comment! pinchcode.com

Updated on August 05, 2022

Comments

  • Pinch
    Pinch almost 2 years

    I created a Code First class

    A database was created in Sql automatically.

    I deleted the tables :(

    I have migrations enabled:

    AutomaticMigrationsEnabled = true;
    AutomaticMigrationDataLossAllowed = true;
    Database.SetInitializer(new DropCreateDatabaseAlways<SurveyContext>());
    

    Yet upon

    update-database -force
    
    Cannot find the object "dbo.CustomerResponses" because it does not exist or you do not have permissions.
    

    Please help.

  • iiminov
    iiminov about 9 years
    Seems to work only if you have migration information defined in your project No migrations configuration type was found in the assembly '{project_name}'.. But it was worth a try.
  • iiminov
    iiminov about 9 years
    handy link to the topic at hand - msdn.microsoft.com/en-us/data/jj591621.aspx
  • jk7
    jk7 almost 9 years
    The downside of this solution is that you lose your migration history.
  • Guerrilla
    Guerrilla almost 8 years
    i tried this but then it wouldn't update because the other tables already exist