在代码优先实体框架中手动编辑数据库

发布于 2024-11-08 18:04:47 字数 357 浏览 0 评论 0原文

我一直在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

街角迷惘 2024-11-15 18:04:47

我也曾为此经历过一些挣扎。我发现当您让 EF 为您创建数据库时,会创建一个名为 dbo.EdmMetadata 的表,这就是 EF 跟踪模型状态的位置/方式。我发现,如果您在数据库最初创建后删除此表,您会将内容置于“手动模式”,您现在可以从数据库中手动添加/删除列、表等,并且 EF 不会抛出您所提示的错误正在看到。

但是,如果您希望在更改模型时继续让 EF 更新数据库,则需要创建并调用继承自 DropCreateDatabaseIfModelChangesDropCreateDatabaseAlways 取决于您希望发生的行为。

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.

和我恋爱吧 2024-11-15 18:04:47

使用新字段名称更改类,删除表“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.

Saygoodbye 2024-11-15 18:04:47

正如我所看到的,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

冷︶言冷语的世界 2024-11-15 18:04:47

我知道这已被标记为已解决,但就我而言,它并没有达到目的。

删除 dbo.EdmMetadata 后,我收到了不同的错误:

无法检查模型兼容性,因为数据库不包含模型元数据。确保 IncludeMetadataConvention 已添加到 DbModelBuilder 约定中。

对我来说,它的工作方式是从 Application_Start 中删除 Initializer 类:

void Application_Start(object sender, EventArgs e)
{
    // MyDB.SetInitializer<MyContext>(new MyInitializer());
}

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 from Application_Start:

void Application_Start(object sender, EventArgs e)
{
    // MyDB.SetInitializer<MyContext>(new MyInitializer());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文