gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 04:32:28 26 4
gpt4 key购买 nike

我正在 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; }
}

}

最佳答案

命令:

  1. 启用迁移默认上下文
  2. add-migration InitialCreate(用于生成快照)
  3. add-migration InitialCreate(应用快照)
  4. 更新数据库
  5. 更新数据库-verbose

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

关于c# - 如何启用迁移以在 MVC4 中更新我的数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17922945/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com