Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Enable Migration to update my database in MVC4?

Tags:

I'm working on a project using MVC4 in Visual Studio 2012 and have added a column in the table.

Now when I want to debug my project the error says to use the migration to update my database.

What I have to do?

I have been searching a lot and found some methods like:

protected override void OnModelCreating(DbModelBuilder modelBuilder) {   Database.SetInitializer<ResTabelaIndex>(null); } 

but don't know how and where to implement this... Have tried in app_start, global.asax etc...

What I found was, to enable the migrations directly in the console from the nuget.

But I can't make this work.

Commands I use:

Enable-Migrations -EnableAutomaticMigrations 

==> Console says that more than one context was found . To enable use, Enable-Migrations -ContextTypeName NameOfTheNamespace.Models.DefaultConnection

But I don't know what is the -ContextTypeName, have tried a lot but couldn't understand.

My Model Code:  using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.Entity; using System.Data.Entity.ModelConfiguration.Conventions; using System.Data.Entity.Migrations; using System.ComponentModel.DataAnnotations; using System.Data.Entity.Infrastructure;  namespace Vista.Models {     public class TabelaIndex     {         public int ID { get; set; }         public string n_empresa { get; set; }         public string titulo{ get; set; }         public string url { get; set; }         public string imagens { get; set; }     }      public class DefaultConnection : DbContext     {         public DbSet<TabelaIndex> ResTabelaIndex { get; set; }     }  } 
like image 861
user2232273 Avatar asked Jul 29 '13 11:07

user2232273


People also ask

How do I update my database in migration?

After creating a migration file using the add-migration command, you have to update the database. Execute the Update-Database command to create or modify a database schema. Use the –verbose option to view the SQL statements being applied to the target database.

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.


2 Answers

Commands:

  1. enable-migrations default context
  2. add-migration InitialCreate (for generating snapshot)
  3. add-migration InitialCreate (to apply snapshot)
  4. update-database
  5. update-database -verbose

Detailed explination will here: http://www.dotnet-tricks.com/Tutorial/entityframework/R54K181213-Understanding-Entity-Framework-Code-First-Migrations.html

like image 137
Pathik Tailor Avatar answered Oct 09 '22 18:10

Pathik Tailor


The error is saying that you have two contexts. When you first create a project using MVC 4, Visual Studio creates a context for your SimpleMembership by default (check Models/Account.cs) or do a ctrl+f for UsersContext, you can just delete this file if you are not using SimpleMembership. After removing this context, go ahead and add the following to your DefaultConnection class:

protected override void OnModelCreating(DbModelBuilder builder) {    Database.SetInitializer(new MigrateDatabaseToLatestVersion<DefaultConnection,Configuration>()); } 

If you enabled migrations correctly you should also have a folder called Migrations and inside it a Configuration class, its constructor should look like this (if you want to enable automatic migrations):

public Configuration() {    AutomaticMigrationsEnabled = true; } 
like image 27
SOfanatic Avatar answered Oct 09 '22 18:10

SOfanatic