在代码优先实体框架中手动编辑数据库 [英] Manually editing database in code first entity framework

查看:27
本文介绍了在代码优先实体框架中手动编辑数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have been trying out EF 4.1 (code first) with MVC 3. I am thinking ahead to when the application will need changes. I tested a couple of scenarios. I like the idea of manually editing the database when the model (my POCOs) would changed.

ASP.NET error when I change the model :

"The model backing the 'CTCMContext' context has changed since the database was created. Either manually delete/update the database..."

Now, it says that I can "manually update the database", but I did and still get the same error. Am I missing something !!?!

EDIT

Does this have to do with the model hash generate by EF ?

解决方案

I have had some struggles with this as well. I found that when you let EF create your database for you that a table named dbo.EdmMetadata is created and this is where/how EF tracks the state of the model. I found that if you delete this table after the database has been initially created you will put things into "manual mode" where you can now manually add/remove columns, tables, etc. from your database and EF will not throw the error that you are seeing.

If however you want to keep having EF update your database as you make changes to your model, you will need to create and call a ContextInitializer class that inherits from either DropCreateDatabaseIfModelChanges or DropCreateDatabaseAlways depending upon the behavior that you want to have happen.

这篇关于在代码优先实体框架中手动编辑数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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