Entity Framework 6 - Does not create tables when enabling Migrations
Asked Answered
E

5

7

In my application I enable Code First Migrations with some migrations, Also I use SQL Server Compact for integration test.

When I run my tests, Entity Framework create an empty database and tries to run migration on that empty database and thrown The specified table does not exist.

Based on this report I think usage of Migration in Entity Framework 6 has changed.

I test all Database Initializer with Context.Database.Create(); but in all case tabale's never created.

Ebba answered 10/5, 2014 at 8:9 Comment(2)
Migrations work as always. You need the MigrateDatabaseToLatestVersion initializer.Indelicacy
I've tested it and table does not created.Ebba
E
18

I don't know that this is EntityFramework's bug or not, but when I made rename the namespace of Migration Configuration class from default (Projectname/Migrations) to any none default name, migration works well.

Ebba answered 4/6, 2014 at 22:45 Comment(6)
It indeed works. Changing the namespace may not be enough, also change the class name. There seems to be some kind of name conflict at runtime...Barilla
That's really really bizarre. It worked for me too. Any explanation to why this works?Vaisya
This is really bad, it took way to long for me to find this post and sure enough it was the problem.Syncrisis
I have changed Configuration class name but still tables were not createdNonconformist
Worked for me too.Laroche
Curiously enough, this also eliminated another error I had where "add-migration" would throw an Error saying 'Sequence contains more than one matching element' (from FindAlteredColumns), which I did not get in another "fresh" project using the same entity classes (where I actually was using the default namespace in Configuration-class).Rutherfordium
H
2

Context.Database.Create() will not execute migrations! It only creates empty db. To Update database from code to latest version you need to use DbMigrator.Update method:

var migrator = new DbMigrator(new MyMigrationsConfiguration());
migrator.Update();

Alternatively you might use MigrateDatabaseToLatestVersion

Database.SetInitializer(new MigrateDatabaseToLatestVersion<BlogContext, Configuration>());

It is described in details here: http://msdn.microsoft.com/en-us/data/jj591621.aspx#initializer

Hambrick answered 10/5, 2014 at 9:10 Comment(0)
A
1

There may be previous migration files which the ide may be referring to mostly likely due to caching. Drop backup and drop target database if it exists, and drop the migration folder. Now add the migration and you will be good to go.

Alasdair answered 30/8, 2022 at 9:51 Comment(0)
E
0

It does happens when adding model and running add-migration command.

Here is the simplest cause of this issue:

Add a newly added model property into IdentityDbContex class.

Here are the steps:

  1. create model
  2. add property into IdentityDbContex class
  3. run add-migration
  4. update-database
Erotogenic answered 11/5, 2017 at 15:10 Comment(0)
U
0

In case someone still struggles to fix the issue.

The code that follows works for me: add-migration MyFirstMigration

Meanwhile add-migration "MyFirstMigration" with the migration name ramped in quote doesn't work.

Unaccountable answered 6/10, 2020 at 10:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.