如何在MVC4中启用迁移来更新我的数据库?我正在VisualStudio2012中使用MVC4开发一个项目,并向表中添加了一列。现在,当我想调试我的项目时,错误提示使用迁移来更新我的数据库。我该怎么办?我一直在搜索并找到了一些方法,例如:protectedoverridevoidOnModelCreating(DbModelBuildermodelBuilder){Database.SetInitializer(null);但不知道如何以及在哪里实现这个...已经尝试过app_start、global.asax等...我发现从nuget中直接在控制台中启用迁移。但我做不到。我使用的命令:Enable-Migrations-EnableAutomaticMigrations==>consolesaysmultiplecontextsfound。要启用使用Enable-Migrations-ContextTypeNameNameOfTheNamespace.Models.DefaultConnection但我不知道什么是-ContextTypeName,尝试了很多但无法理解。我的模型代码:使用系统;使用System.Collections.Generic;使用System.Linq;使用System.Web;使用System.Data.Entity;;使用System.ComponentModel.DataAnnotations;使用System.Data.Entity.Infrastructure;namespaceVista.Models{publicclassTabelaIndex{publicintID{get;放;}publicstringn_empresa{get;放;}publicstringtitulo{get;放;}公共字符串url{得到;放;}公共字符串图像{得到;放;}}publicclassDefaultConnection:DbContext{publicDbSetResTabelaIndex{get;放;}}}错误是说你有两个上下文。当您第一次使用MVC4创建项目时,VisualStudio默认为您的SimpleMembership创建一个上下文(检查Models/Account.cs)或为UsersContext执行ctrl+f,如果您不使用SimpleMembership,则可以删除此文件。删除此上下文后,继续将以下内容添加到DefaultConnection类:如果你正确启用了迁移,还应该有一个名为Migrations文件夹的函数,其中包含一个Configuration类,其构造函数应该如下所示(如果你想启用自动迁移):publicConfiguration(){AutomaticMigrationsEnabled=true;}命令:enable-migrationsdefaultcontextadd-migrationInitialCreate(用于生成快照)add-migrationInitialCreate(应用快照)update-databaseupdate-database-verbose详细探索将在这里:http://www.dotnet-tricks。com/Tutorial/entityframework/R54K181213-Understanding-Entity-Framework-Code-First-Migrations.html尝试在控制台中输入:Enable-Migrations-ContextTypeNameVista.Models.DefaultConnectionVista.Models.DefaultConnection是您的上下文(继承来自DbContext)。如果您有小的更改,则不需要迁移。您可以使用sql脚本向数据库中的任何表添加列,向模型添加属性并删除元数据表。(毫无疑问,首先备份数据库)。或者你可以像这样使用迁移:aspnet-mvc-4-entity-framework-scaffolding-and-migrations以上是C#学习教程:HowtoenablemigrationstoupdatemydatabaseinMVC4?如果所有分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
