Add new table to an existing database using Code First approach in EF 6 and MVC 5

扶醉桌前 提交于 2019-12-02 18:35:26

What has happened here is that you had existing database tables before Migrations where enabled. So Entity Framework thinks that ALL your database objects need to created, this could be seen by opening up your PostMigration migration file.

The best way is to trick EF into to doing the initial migration with every before you added the Post table and then do the Posts migration after.

Steps

  1. Comment out the Posts in the DBContext so EF doesn't know about posts

    //public DbSet<Post> Posts { get; set; }
    
  2. Enable Migrations

    Enable-Migrations
    
  3. Add an initial migration with all tables prior to your changes

    Add-Migration "InitialBaseline" –IgnoreChanges
    
  4. Now update the database, this will create a MigrationHistory table so that EF knows what migrations have been run.

    Update-Database
    
  5. Now you can uncomment the line in 1 to "Do your change"

  6. Create a new migration with the addition of Posts

    Add-Migration "PostMigration"
    
  7. Now do an update... and hopefully it should all work.

    Update-Database
    

Now that migrations are all setup and they are baselined future migrations will be easy.

For more information I found this link useful: http://msdn.microsoft.com/en-us/data/dn579398.aspx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!