Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute Code First Migrations is Grayed Out in Publish Settings

Using Windows Azure and attempting to publish my MVC3 Application. The check box for Execute Code First Migration in the settings panel of the Publish web application is grayed out. What changes do I need to make to be able to enable it?

like image 744
ek_ny Avatar asked Sep 19 '12 13:09

ek_ny


People also ask

How do I enable migrations in Visual Studio?

From the Tools menu, select NuGet Package Manager > Package Manager Console. The enable-migrations command creates a Migrations folder in the ContosoUniversity project, and it puts in that folder a Configuration. cs file that you can edit to configure Migrations.

What is Code First Migrations?

Code First Migrations allow you to create a new database or to update existing database based on your model classes by using Package Manager Console exist within Visual Studio 2013 or Visual Studio 2012.


2 Answers

I believe you see the following "Execute Code First Migration" disabled when you try to publish your MVC application:

enter image description here

This is potentially because either you do not full code written for Code migration in your application as well no or incorrect DB setup in your web.config as described here.

In order to have Code Migration enabled, you must have a DB configured (in case of Windows Azure you need to provide SQL Database info in the web.config) in web.config and a complete class is written on how the code migration will happen depend on your model. Here is an example on how to achieve it.

http://msdn.microsoft.com/en-us/library/dd394698#efcfmigrations

like image 134
AvkashChauhan Avatar answered Oct 07 '22 04:10

AvkashChauhan


I am assuming that you have Entity Framework model and in your database already (if not then you need to do some reading, answer by @AvkashChauhan would be indeed a good starting point).

However if you do have a model and all the configurations like:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
   modelBuilder.Configurations.Add(new YourEntityMap());
}

and all the entity mappings like:

public class YourEntityMap : EntityTypeConfiguration<YourEntity>
{
    public YourEntityMap()
    {
        this.HasKey(t => t.Id);
    }
}

and you still don't get the darn checkbox enabled you might want to do following steps:

Go to Tools > NuGet Package Manager > Package Manager Console

enter image description here

Then in console write

Enable-Migrations -ContextTypeName Company.Models.YourDevContext

where Company.Models.YourDevContext is your Database Context (look for class that inherits from DbContext should be same one that has OnModelCreating override).

after running command you should get something like:

enter image description here

At this point you should have Migrations folder added to the solution more on how to handle migrations here

Hope this saves you some time.

like image 33
Matas Vaitkevicius Avatar answered Oct 07 '22 03:10

Matas Vaitkevicius