我正在使用Visual Studio 2012中的MVC4工作,并在表中添加了一列。如何启用迁移以更新MVC4中的数据库?
现在,当我想调试我的项目时,错误表示使用迁移来更新我的数据库。
我该怎么办?
我一直在寻找了很多,发现像一些方法:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer<ResTabelaIndex>(null);
}
,但不知道如何和在哪里实施这个......曾试图app_start,Global.asax的等等
我发现的是,从nuget直接在控制台中启用迁移。
但我不能做这项工作。
命令我使用:
Enable-Migrations -EnableAutomaticMigrations
==> consolse说,不止一个背景下被发现。 要启用,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; }
}
}
FWIW,我需要添加“ base.OnModelCreating(助洗剂);”到您的OnModelCreating覆盖的最后,以便让自动迁移在我的生产服务器上运行(使用EF 6)。 –