在代码优先实体框架中手动编辑数据库
我一直在尝试使用 MVC 3 的 EF 4.1(代码优先)。我正在提前考虑应用程序何时需要更改。我测试了几个场景。我喜欢在模型(我的 POCO)发生变化时手动编辑数据库的想法。
当我更改模型时出现 ASP.NET 错误:
“自创建数据库以来,支持“CTCMContext”上下文的模型已更改。手动删除/更新数据库...”
现在,它说我可以“手动更新数据库”,但是我这样做了,但仍然遇到相同的错误。我是不是错过了什么!!!
编辑
这与 EF 生成的模型哈希有关吗?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我也曾为此经历过一些挣扎。我发现当您让 EF 为您创建数据库时,会创建一个名为 dbo.EdmMetadata 的表,这就是 EF 跟踪模型状态的位置/方式。我发现,如果您在数据库最初创建后删除此表,您会将内容置于“手动模式”,您现在可以从数据库中手动添加/删除列、表等,并且 EF 不会抛出您所提示的错误正在看到。
但是,如果您希望在更改模型时继续让 EF 更新数据库,则需要创建并调用继承自 DropCreateDatabaseIfModelChanges 或 DropCreateDatabaseAlways 取决于您希望发生的行为。
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.
使用新字段名称更改类,删除表“EdmMetaData”,然后重新编译您的应用程序。
您将负责修改您的视图上的字段名称。
它对我有用。
change the class with the new fieldnames, delete the table "EdmMetaData" then recompile your app.
You will be responsible on modifying the fieldname on your views.
it works for me.
正如我所看到的,EF 中实际上没有任何用于代码优先数据演化的内置方法。
对于我最初的问题,答案在于删除模式生成/验证。只有这样,手动编辑代码和数据库才可能起作用。
更新:
EF 5.0 现在支持迁移
As I can see, there aren't really any methods built-in EF for code-first data evolution.
For what was of my initial question, the answer lies in removing the schema generation/validation. It is only then that manually editing the code and database may work.
UPDATE :
EF 5.0 now support migrations
我知道这已被标记为已解决,但就我而言,它并没有达到目的。
删除 dbo.EdmMetadata 后,我收到了不同的错误:
无法检查模型兼容性,因为数据库不包含模型元数据。确保 IncludeMetadataConvention 已添加到 DbModelBuilder 约定中。
对我来说,它的工作方式是从
Application_Start
中删除Initializer
类:I know this has been marked as solved but in my case it didn't do the trick.
Once I deleted
dbo.EdmMetadata
I was getting a different error:Model compatibility cannot be checked because the database does not contain model metadata. Ensure that IncludeMetadataConvention has been added to the DbModelBuilder conventions.
The way it worked for me was to remove the
Initializer
class fromApplication_Start
: