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?
问题:
回答1:
I believe you see the following "Execute Code First Migration" disabled when you try to publish your MVC application:
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
回答2:
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
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:
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.