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

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

问题描述

我已经尝试使用MVC 3的EF 4.1(代码第一个)。我正在考虑应用程序何时需要更改。我测试了几个场景。我喜欢在模型(我的POCO)更改时手动编辑数据库的想法。



当我更改模型时,ASP.NET错误:



支持CTCMContext '从创建数据库以来,上下文已经发生变化,手动删除/更新数据库...



现在,它说我可以手动更新数据库,但我已经做了,仍然得到相同的错误。我错过了什么!!?



编辑



这是否与EF的模型哈希生成有关?

解决方案

我也有一些斗争。我发现,当您让EF为您创建一个名为dbo.EdmMetadata的表创建数据库时,这就是EF如何跟踪模型的状态。我发现如果在数据库最初创建之后删除此表,您将把事情变成手动模式,您现在可以从数据库手动添加/删除列,表等,而EF不会抛出您的错误正在看。



如果您想在更改模型时继续使用EF更新数据库,则需要创建并调用继承自 DropCreateDatabaseIfModelChanges DropCreateDatabaseAlways 取决于您想要发生的行为。


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天全站免登陆