Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create database using code first migrations?

I'm having ASP.NET MVC 3 project that uses Entity Framwork 4.3 and its migrations. Now I want Entity Framework to create a database for me using migrations that I have already. When I trying to run Update-Database script it gives me the following:

Update-Database -Verbose -ProjectName AssemblyWithMigrations -StartUpProjectName WebProjectAssembly No migrations configuration type was found in the assembly '/* my name of assembly */'. (In Visual Studio you can use the Enable-Migrations command from Package Manager Console to add a migrations configuration).

But, when I'm trying to run Enable-Migrations I see following:

Migrations have already been enabled in project 'AssemblyWithMigrations '. To overwrite the existing migrations configuration, use the -Force parameter.

So, the problem is EF trying to resolve current migration version to update database, I suppose. But database doesn't exist and it obviously fails.

The question is: how to use EF migrations for creating database if it doesn't exist? Or, what is the right way to do that using Nuget console?

In summary, what I want: 1. run command (perhaps update-database) that will create database using my web.config file 2. all migrations will be applied on the created database in their creation order.

Thanks. :)

like image 852
sigurd Avatar asked May 23 '12 11:05

sigurd


People also ask

How do I code my first migration to an existing database?

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.

What is used to create a database that is being created by code first or evolved by migrations?

Code First Migrations is the recommended way to evolve your application's database schema if you are using the Code First workflow. Migrations provide a set of tools that allow: Create an initial database that works with your EF model. Generating migrations to keep track of changes you make to your EF model.


2 Answers

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.

like image 62
Dave R Avatar answered Oct 19 '22 12:10

Dave R


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>());
}                                                                                                                                                                                                                                                   
like image 21
njabs Avatar answered Oct 19 '22 12:10

njabs