asp.net-mvc 支持数据库上下文的模型已更改;考虑代码优先迁移

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14948205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:36:40  来源:igfitidea点击:

Model backing a DB Context has changed; Consider Code First Migrations

asp.net-mvcentity-framework

提问by Brian D

The model backing the 'MyDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).

自创建数据库以来,支持“MyDbContext”上下文的模型已更改。考虑使用 Code First 迁移来更新数据库 ( http://go.microsoft.com/fwlink/?LinkId=238269)。

What causes this to happen? I've literally just created a brand new database and have changed nothing, but every time I try to access a model from a controller it throws this.

是什么导致这种情况发生?我实际上只是创建了一个全新的数据库并且什么也没改变,但是每次我尝试从控制器访问模型时,它都会抛出这个。

Edit

编辑

It has something to do with the fact that I was attempting to share a connection string (i.e. a database) with two separate entities.

这与我试图与两个单独的实体共享连接字符串(即数据库)的事实有关。

采纳答案by AaronLS

EF codefirst will look at your DbContext, and discover all the entity collections declared in it(and also look at entities related to those entities via navigation properties). It will then look at the database you gave it a connection string to, and make sure all of the tables there match the structure of your entities in model. If they do not match, then it cannot read/write to those tables. Anytime you create a new database, or if you change something about the entity class declarations, such as adding properties or changing data types, then it will detect that the model and the database are not in sync. By default it will simply give you the above error. Usually during development what you want to happen is for the database to be recreated(wiping any data) and generated again from your new model structure.

EF codefirst 将查看您的 DbContext,并发现其中声明的所有实体集合(并通过导航属性查看与这些实体相关的实体)。然后它会查看你给它一个连接字符串的数据库,并确保那里的所有表都匹配模型中实体的结构。如果它们不匹配,则它无法读取/写入这些表。任何时候你创建一个新的数据库,或者如果你更改了实体类声明的某些内容,例如添加属性或更改数据类型,那么它会检测到模型和数据库不同步。默认情况下,它只会给你上面的错误。通常在开发过程中,您想要发生的是重新创建数据库(擦除任何数据)并从您的新模型结构再次生成。

To do that, see "RecreateDatabaseIfModelChanges Feature" in this article: http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx

为此,请参阅本文中的“RecreateDatabaseIfModelChanges 功能”:http: //weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx

You basically need to provide a database initializer that inherits from DropCreateDatabaseIfModelChanges (RecreateDatabaseIfModelChanges is now deprecated). To do this, simply add this line to the Application_Startmethod of your Global.asaxfile.

您基本上需要提供一个从 DropCreateDatabaseIfModelChanges 继承的数据库初始值设定项(现在不推荐使用 RecreateDatabaseIfModelChanges)。为此,只需将此行添加到文件的Application_Start方法中Global.asax

Database.SetInitializer<NameOfDbContext>(new DropCreateDatabaseIfModelChanges<NameOfDbContext>());

Once you go to production and no longer want to lose data, then you'd remove this initializer and instead use Database Migrations so that you can deploy changes without losing data.

一旦您进入生产环境并且不再希望丢失数据,那么您将删除此初始化程序并改为使用数据库迁移,以便您可以在不丢失数据的情况下部署更改。

回答by Craig Fisher

In my case this error was caused by the existence of the _MigrationsHistory table in the database. Deleting that table fixed the problem. Not sure how that table got into our test environment database.

在我的例子中,这个错误是由数据库中 _MigrationsHistory 表的存在引起的。删除该表解决了问题。不确定该表是如何进入我们的测试环境数据库的。

回答by Sid

To solve this error write the the following code in Application_Start() Method in Global.asax.cs file

要解决此错误,请在 Global.asax.cs 文件的 Application_Start() 方法中编写以下代码

Database.SetInitializer<MyDbContext>(null);

回答by Illidan

In case you did changes to your context and you want to manually make relevant changes to DB (or leave it as is), there is a fast and dirty way.

如果您对上下文进行了更改,并且想要手动对 DB 进行相关更改(或保持原样),则有一种快速而肮脏的方法。

Go to DB, and delete everything from "_MigrationHistory" table

转到数据库,并从“_MigrationHistory”表中删除所有内容

回答by Photonic

Easiest and Safest MethodIf you know that you really want to change/update your data structure so that the database can sync with your DBContext, The safest way is to:

最简单和最安全的方法如果您知道您确实想要更改/更新您的数据结构,以便数据库可以与您的 DBContext 同步,最安全的方法是:

  1. Open up your Package Manager Console
  2. Type: update-database -verbose -force
  1. 打开你的包管理器控制台
  2. 类型:更新数据库 -verbose -force

This tells EF to make changes to your database so that it matches your DBContext data structure

这告诉 EF 对您的数据库进行更改,使其与您的 DBContext 数据结构相匹配

回答by arsal khan

You can fix the issue by deleting the __MigrationHistorytable which is created automatically in the database and logs any update in the database using code-first migrations. Here, in this case, you manually changed your database while EF assumed you had to do it with the migration tool. Deleting the table means to the EF that there are no updates and no need to do code-first migrations thus it works perfectly fine.

您可以通过删除__MigrationHistory在数据库中自动创建的表并使用代码优先迁移记录数据库中的任何更新来解决此问题。在这里,在这种情况下,您手动更改了数据库,而 EF 假定您必须使用迁移工具来完成。删除表对 EF 意味着没有更新,也不需要进行代码优先迁移,因此它可以正常工作。

回答by tyne

Just go to Package Manage Console, type the following:

只需转到包管理控制台,键入以下内容:

Enable-Migrations

启用迁移

*If the error like this appears "Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration."

*如果出现这样的错误“无法更新数据库以匹配当前模型,因为存在挂起的更改并且自动迁移被禁用。将挂起的模型更改写入基于代码的迁移或启用自动迁移。”

Do this >>> Add-Migration

这样做 >>>添加迁移

*Visual studio will ask for a name, kindly input the name you want.

*Visual Studio 会询问名称,请输入您想要的名称。

Update-Database

更新数据库

回答by Eldiyar Talantbek

Entity Framework detects something about the model has changed, you need to do something to the database to get this work. Solution: 1. enable-migrations 2. update-database

Entity Framework 检测到有关模型的某些内容已更改,您需要对数据库执行某些操作才能使其工作。解决办法:1。启用迁移2. 更新数据库

回答by Antonio Ooi

This happens when your table structure and model class no longer in sync. You need to update the table structure according to the model class or vice versa -- this is when your data is important and must not be deleted. If your data structure has changed and the data isn't important to you, you can use the DropCreateDatabaseIfModelChangesfeature (formerly known as 'RecreateDatabaseIfModelChanges' feature) by adding the following code in your Global.asax.cs:

当您的表结构和模型类不再同步时,就会发生这种情况。您需要根据模型类更新表结构,反之亦然——这是您的数据很重要且不得删除的时候。如果您的数据结构已更改并且数据对您不重要,您可以通过在Global.asax.cs 中添加以下代码来使用该DropCreateDatabaseIfModelChanges功能(以前称为“ RecreateDatabaseIfModelChanges”功能):

Database.SetInitializer<MyDbContext>(new DropCreateDatabaseIfModelChanges<MyDbContext>());

Run your application again.

再次运行您的应用程序。

As the name implies, this will drop your database and recreate according to your latest model class (or classes) -- provided you believe the table structure definitions in your model classes are the most current and latest; otherwise change the property definitions of your model classes instead.

顾名思义,这将删除您的数据库并根据您最新的模型类(或多个类)重新创建——前提是您相信模型类中的表结构定义是最新的;否则,请改为更改模型类的属性定义。

回答by Ranjan

If you have changed the model and database with tables that already exist, and you receive the error "Model backing a DB Context has changed; Consider Code First Migrations" you should:

如果您使用已存在的表更改了模型和数据库,并且收到错误“支持数据库上下文的模型已更改;考虑代码优先迁移”,您应该:

  • Delete the files under "Migration" folder in your project
  • Open Package Manager console and run pm>update-database -Verbose
  • 删除项目中“迁移”文件夹下的文件
  • 打开包管理器控制台并运行 pm>update-database -Verbose