Entity Framework 6 - Does not create tables when enabling Migrations

前端 未结 4 1717
生来不讨喜
生来不讨喜 2021-01-05 10:13

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 crea

相关标签:
4条回答
  • 2021-01-05 10:40

    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
    0 讨论(0)
  • 2021-01-05 10:49

    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.

    0 讨论(0)
  • 2021-01-05 10:56

    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.

    0 讨论(0)
  • 2021-01-05 10:57

    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

    0 讨论(0)
提交回复
热议问题