How to Enable Migration to update my database in MVC4?

前端 未结 4 1483
时光取名叫无心
时光取名叫无心 2021-01-01 15:03

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 migrat

相关标签:
4条回答
  • 2021-01-01 15:14

    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

    0 讨论(0)
  • 2021-01-01 15:16

    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;
    }
    
    0 讨论(0)
  • 2021-01-01 15:23

    If you have small changes not need migration. You can add column to any table on database with sql script and add property to model and delete metadata table. (back up database firstly no doubt).

    Or you can use Migrations like this: aspnet-mvc-4-entity-framework-scaffolding-and-migrations

    0 讨论(0)
  • 2021-01-01 15:27

    Try typing this into the console:

    Enable-Migrations -ContextTypeName Vista.Models.DefaultConnection
    

    Vista.Models.DefaultConnection is your context (the class that inherits from DbContext).

    0 讨论(0)
提交回复
热议问题