如何使迁移到更新我在MVC4数据库? [英] How to Enable Migration to update my database in MVC4?

查看:170
本文介绍了如何使迁移到更新我在MVC4数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作在Visual Studio 2012中使用MVC4一个项目,并已加入该表中的列。

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

我该怎么办?

我一直在寻找了很多,找到了一些方法,如:

 保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
{
  Database.SetInitializer< ResTabelaIndex>(NULL);
}

但不知道如何以及在哪里实现这个......曾试图app_start,Global.asax中等等......

但是我发现了,直接使迁移从的NuGet控制台。

但我不能做这项工作。

命令我使用的:

 启用的迁移-EnableAutomaticMigrations

==> consolse说多个上下文被发现。
       要启用使用启用的迁移-ContextTypeName NameOfTheNamespace.Models.DefaultConnection

但我不知道什么是 -ContextTypeName ,已经尝试了很多,但couldn't理解。

 我的模型code:使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Data.Entity的;
使用System.Data.Entity.ModelConfiguration.Conventions;
使用System.Data.Entity.Migrations;
使用System.ComponentModel.DataAnnotations;
使用System.Data.Entity.Infrastructure;命名空间Vista.Models
{
    公共类TabelaIndex
    {
        公众诠释ID {搞定;组; }
        公共字符串n_em $ P $ {PSA得到;组; }
        公共字符串TITULO {搞定;组; }
        公共字符串URL {搞定;组; }
        公共字符串imagens {搞定;组; }
    }    公共类DefaultConnection:的DbContext
    {
        公共DbSet< TabelaIndex> ResTabelaIndex {搞定;组; }
    }}


解决方案

该错误是说你有两个背景。当你第一次创建一个使用MVC 4项目,Visual Studio创建默认为你的 SimpleMembership 上下文(检查型号/ Account.cs),或者做一个按Ctrl + F UsersContext ,你可以,如果你使用的不是删除此文件 SimpleMembership
除去此背景下后,继续前进,以下内容添加到您的 DefaultConnection 类:

 保护覆盖无效OnModelCreating(DbModelBuilder建设者)
{
   Database.SetInitializer(新MigrateDatabaseToLatestVersion< D​​efaultConnection,结构>());
}

如果您启用迁移正确,你也应该叫一个文件夹迁移和里面一个配置类,它的构造函数应该是这样的(如果你想启用自动迁移):

 公共配置()
{
   AutomaticMigrationsEnabled = TRUE;
}

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.

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.asax etc...

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

But I can´t make this work.

Commands I use:

Enable-Migrations -EnableAutomaticMigrations

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

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

}

解决方案

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

这篇关于如何使迁移到更新我在MVC4数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆