【问题标题】:How to Enable Migration to update my database in MVC4?如何启用迁移以更新 MVC4 中的数据库?
【发布时间】:2013-07-29 04:17:07
【问题描述】:

我正在开发一个在 Visual Studio 2012 中使用 MVC4 的项目,并在表格中添加了一列。

现在当我想调试我的项目时,错误提示使用迁移来更新我的数据库。

我必须做什么?

我搜索了很多,发现了一些方法,例如:

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

但不知道如何以及在哪里实现这个...在app_startglobal.asax 等中尝试过...

我发现,直接在控制台中从 nuget 启用迁移。

但我做不到。

我使用的命令:

Enable-Migrations -EnableAutomaticMigrations

==> 控制台说找到了多个上下文。 要启用使用,Enable-Migrations -ContextTypeName NameOfTheNamespace.Models.DefaultConnection

但是不知道-ContextTypeName是什么,试了很多还是看不懂。

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; }
    }

}

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-mvc-4 migration


    【解决方案1】:

    命令:

    1. enable-migrations default context
    2. add-migration InitialCreate(用于生成快照)
    3. add-migration InitialCreate(应用快照)
    4. update-database
    5. update-database -verbose

    这里会详细解释: http://www.dotnet-tricks.com/Tutorial/entityframework/R54K181213-Understanding-Entity-Framework-Code-First-Migrations.html

    【讨论】:

    • 能否请edit 解释为什么这些命令回答了这个问题?纯代码答案是discouraged,因为它们不教授解决方案。 (该帖子至少被一位用户标记,大概是因为他们认为应该删除没有解释的答案。)
    【解决方案2】:

    错误是说您有两个上下文。当您第一次使用 MVC 4 创建项目时,Visual Studio 默认为您的 SimpleMembership 创建一个上下文(检查 Models/Account.cs)或为 ctrl+f 执行 UsersContext,如果您是不使用SimpleMembership。 删除此上下文后,继续将以下内容添加到您的 DefaultConnection 类中:

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

    如果您正确启用了迁移,您还应该有一个名为 Migrations 的文件夹,其中有一个 Configuration 类,它的构造函数应该如下所示(如果您想启用自动迁移):

    public Configuration()
    {
       AutomaticMigrationsEnabled = true;
    }
    

    【讨论】:

    • FWIW,我需要添加“base.OnModelCreating(builder);”到 OnModelCreating 覆盖的末尾,以便让自动迁移在我的生产服务器上工作(使用 EF 6)。
    【解决方案3】:

    尝试在控制台中输入:

    Enable-Migrations -ContextTypeName Vista.Models.DefaultConnection
    

    Vista.Models.DefaultConnection 是您的上下文(继承自 DbContext 的类)。

    【讨论】:

      【解决方案4】:

      如果您有小的更改,则不需要迁移。您可以使用 sql 脚本将列添加到数据库上的任何表,并将属性添加到模型和删除元数据表。 (毫无疑问,首先备份数据库)。

      或者您可以像这样使用迁移:aspnet-mvc-4-entity-framework-scaffolding-and-migrations

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-10
        • 2017-05-26
        • 2013-12-02
        • 1970-01-01
        • 1970-01-01
        • 2021-07-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多