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

How to Enable Migration to update my database in MVC4?(如何启用迁移以更新 MVC4 中的数据库?)
本文介绍了如何启用迁移以更新 MVC4 中的数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我必须做什么?

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

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

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

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

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

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

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

但我做不到.

我使用的命令:

Enable-Migrations -EnableAutomaticMigrations

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

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

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

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

}

推荐答案

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

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

如果您正确启用了迁移,您还应该有一个名为 Migrations 的文件夹,其中有一个 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;
}

这篇关于如何启用迁移以更新 MVC4 中的数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)