如果在DbContext中更改model-class,是否重建数据库? [英] if change model-class in DbContext, do I rebuild the database?

查看:108
本文介绍了如果在DbContext中更改model-class,是否重建数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的应用程序在MVC与DBContext,在我的本地主机在SQL,我的数据库有数据。然后我需要在一个表中添加一个新的列,在我的模型类中添加我的新成员,并在SQL中修改了我的表,但是当我执行我的App:..上下文已经改变...考虑使用代码首先迁移.. 。

解决方案

@anAgent已经覆盖了这里的答案。我想给你更多关于这里发生的情况的信息,所以你可以理解为什么这些步骤是必要的。



为了生成迁移,实体框架创建一个跟踪应用程序实体状态的 _MigrationHistory 表。每次运行迁移时,都会在此表中添加一个新的行,其中包含散列版本的模式。启动时,实体框架使用此存储状态与实体的当前状态进行比较,如果不匹配,则会导致该错误告诉您模型支持已更改。这意味着为了使您的应用程序代码能够使用底层数据库,需要进行模式修改或将出现SQL错误。



手动完成了对数据库的适当更改,这足以使应用程序正常运行。然而,由于这个 _MigrationHistory 表仍然是旧状态作为最新状态,所以实体框架仍然认为需要更新模式。再次,它不是看你的数据库的当前状态;在最后一次成功迁移之后,它正在查看数据库的状态。



所有这些都归结为你必须:


  1. 跟随@ anAgent的建议,实际上使用迁移进行更改,而不是手动更改模式。


  2. 删除 _MigrationHistory 表,并继续手动更新您的模式。如果此表不存在,则实体框架将数据库有效地视为现有数据库,并且不会针对该数据库运行迁移,甚至提示您进行迁移。然而,这使球完全在你的法庭上。您将100%负责管理模式,如果您对应用程序进行了更改并忽略了适当更新数据库架构,则应用程序将引发SQL错误异常。



I have my App in MVC with DBContext, in my localhost in SQL, my DB have data. Then I need add a new Column in one Table, add my new member in my model-class and I modified my Table in SQL but when I execute my App: ".. context has changed ... Consider using code first migrations ..."

解决方案

@anAgent has you covered for the answer here. I want to give you more information on what's happening here, so you can understand why those steps are necessary.

In order to generate migrations, Entity Framework creates a _MigrationHistory table that tracks the state of your application's entities. Each time a migration is run, a new row is added to this table with a hashed version of the schema at that point. When starting up, Entity Framework uses this stored state to compare with the current state of your entities, and if they don't match, you get that error telling you that you the model backing as changed. That means that in order for your application code to be able to work with the underlying database, schema modifications need to be made or there will be SQL errors.

What you've done is manually made the appropriate changes to your database, which is enough to make the application function properly. However, since this _MigrationHistory table still has the old state as the latest state, Entity Framework still thinks that the schema needs to be updated. Again, it's not looking at the current state of your database; it's looking at the state of the database after the last successful migration.

What all that boils down to is you must either:

  1. Follow @anAgent's advice and actually use a migration to make the changes instead of manually altering your schema, always.

  2. Remove the _MigrationHistory table and continue to manually update your schema. If this table doesn't exist, Entity Framework effectively treats the database as an existing database and won't ever run migrations against it or even prompt you to. However, this puts the ball entirely in your court. You will be 100% responsible for managing the schema, and if you make changes to the application and neglect to also update the database schema appropriately, the application will raise SQL error exceptions.

这篇关于如果在DbContext中更改model-class,是否重建数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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