Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework 6 - Does not create tables when enabling Migrations

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.

like image 240
Majid Shamkhani Avatar asked May 10 '14 08:05

Majid Shamkhani


People also ask

How do I enable migrations in Entity Framework Core?

Migrations are enabled by default in EF Core. They are managed by executing commands. If you have Visual Studio, you can use the Package Manager Console (PMC) to manage migrations. Alternatively, you can use a command line tool to execute Entity Framework CLI commands to create a migration.

How do I update an existing database using migration?

Run the Add-Migration InitialCreate command in Package Manager Console. This creates a migration to create the existing schema. Comment out all code in the Up method of the newly created migration. This will allow us to 'apply' the migration to the local database without trying to recreate all the tables etc.


2 Answers

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.

like image 106
Majid Shamkhani Avatar answered Oct 22 '22 06:10

Majid Shamkhani


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

like image 39
mr100 Avatar answered Oct 22 '22 08:10

mr100