C# 如何启用迁移以在 MVC4 中更新我的数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17922945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:50:11  来源:igfitidea点击:

How to Enable Migration to update my database in MVC4?

c#asp.netasp.net-mvcasp.net-mvc-4migration

提问by user2232273

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

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

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.asaxetc...

但不知道如何以及在何处实现这一点......已经尝试过app_startglobal.asax等等......

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

我发现的是,直接在控制台中从 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

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

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

但我不知道是什么-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; }
    }

}

采纳答案by SOfanatic

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 SimpleMembershipby default (check Models/Account.cs) or do a ctrl+ffor 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 DefaultConnectionclass:

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

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

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

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

public Configuration()
{
   AutomaticMigrationsEnabled = true;
}

回答by Jeyhun Rahimov

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).

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

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

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

回答by OJ Raque?o

Try typing this into the console:

尝试在控制台中输入以下内容:

Enable-Migrations -ContextTypeName Vista.Models.DefaultConnection

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

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

回答by Pathik Tailor

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
  1. enable-migrations default context
  2. add-migration InitialCreate(用于生成快照)
  3. add-migration InitialCreate(应用快照)
  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

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