How to create database using code first migrations?

泪湿孤枕 提交于 2019-11-28 21:18:35

I realise this is an old question, but what the hell....

To enable migrations to create the initial database I've found the following works well.

  1. Delete your database from within SQL Server Object Explorer
  2. Delete all your existing migrations from within the Migrations folder.
  3. In Package-Management-Console type "Add-Migration InitialCreate"

    [optional, depending on your database initializer]

  4. In Package-Management-Console type "update-database"

This will give you a single migration script which will take you from "no database" to having a database that matches your current model.

njabs

Using Migrations is very useful but at times I have found it's better to use the following statements when working with databases.

Database initialization strategies: use any of the commented statements relevant to you.

public DataContext(): base("DefaultConnection") 
{
  // Database.SetInitializer<DataContext>(new CreateDatabaseIfModelChanges<DataContext>());
  // Database.SetInitializer<DataContext>(new CreateDatabaseIfNotExists<DataContext>());                                                            
  // Database.SetInitializer<DataContext>(new DropCreateDatabaseAlways<DataContext>());
}                                                                                                                                                                                                                                                   
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!