实体框架code-第一:如何手动更新数据库? [英] Entity Framework Code-First: How to manually update the database?

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

问题描述

我已经建立一个小的WPF演示应用程序,它使用EF code-首先要保存其数据的SQL CE 4.0数据库。它工作正常,除非我从一个模型对象中删除一个属性。例如,如果我从这个类中删除HosteBy......

I've build a little WPF demo app which uses EF Code-First to save its data in a SQL CE 4.0 DB. It works fine unless I remove a property from a model object. For example, if I remove "HosteBy" from this class.....

public class Dinner
{
    public int DinnerID { get; set; }
    public string Title { get; set; }   
    public DateTime EventDate { get; set; }
    public string Address { get; set; }
    public string HostedBy { get; set; }

    public virtual ICollection<RSVP> RSVPs { get; set; }
}

...这引发此异常:

...it throws this exception:

该模式支持的NerdDinners背景已经改变,因为在创建数据库。无论是手动删除/更新数据库,或拨打Database.SetInitializer与IDatabaseInitializer实例。例如,该策略DropCreateDatabaseIfModelChanges将自动删除并重新创建数据库,并任选地与新的数据种子它

The model backing the 'NerdDinners' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data.

甚至从数据库中手动删除字段HosteBy后,错误仍然存​​在。我在想什么吗?我必须删除/截断DB还是有另一种解决方案?

The error persists even after removing the field "HosteBy" manually from the database. What am I missing here? Do I have to delete/truncate the db or is there another solution?

推荐答案

在那里,你改变了code第一种模式,你去和手动修改数据库之前,第一个场景,答案是打开(的NuGet)包管理器控制台,然后键入:

In the first scenario where you changed the Code First Model, before you went and modified the database manually, the answer is to open the (Nuget) Package Manager Console and type:

update-database -verbose

除 - 因为在这种情况下,要删除一个列,这将报告说,它是关于删除的东西,并没有你明确说这是确定它不会删除任何东西。所以你键入:

Except - because in this case you are removing a column this will report that it's about to delete something, and it won't delete anything without you explicitly saying that's OK. So you type:

update-database -f -verbose

现在这将删除你在你的模型有列。 -verbose 表示,向你展示它运行的SQL。如果你害怕只是让它删除的东西,而检查的SQL运行之前,使用:

Now this will delete the column you had in your Model. -verbose says to show you the SQL it runs. If you're scared of just letting it delete things and rather inspect the SQL before it runs, use:

更新数据库-f -script

update-database -f -script

这反而转储SQL出一个脚本,你可以查看,并手动运行自己。

That will instead dump the SQL out to a script you can look over, and run manually yourself.

在你在哪里,并手动删除数据库中的列的情况下,你现在有一个更复杂的场景在你的手中;在这里对方的回答中描述的EdmMetadata表包含整个数据库,现在不数据库本身匹配的散列。您可以运行手动SQL的数据库恢复到实体框架期望的方式(该方式是在你手动修改它,而把它带回符合哈希),通过检查你有什么之前,什么你的数据库当前的模样。

In the case where you went on and deleted the column in the database manually, you now have a more complex scenario on your hands; the EdmMetadata table described in the other answer here contains a hash of the entire database that now does not match the database itself. You can run manual SQL to return the DB to the way Entity Framework expects (the way it was before you manually modified it, which brings it back in line with the hash) by inspecting what you had before and what your db currently looks like.

如果这是不可行的,你现在在实体框架$ C C首先$最丑陋的部分。你需要消除哈希表和反向数据库工程师到code文件。

If that's not feasible you are now in the ugliest part of Entity Framework Code First. You need to eliminate the hash table and reverse engineer the db into code files.

哈希表名称取决于EF的版本。在老EF4就像你问,这就是所谓的EdmMetadata。在新的EF5,这就是所谓的__MigrationHistory(在系统表在数据库中,如果您正在寻找在SQL Server Management Studio中)。你需要消灭它。

The hash table name depends on the version of EF. In older EF4 like you were asking about, it's called EdmMetadata. In newer EF5, it's called __MigrationHistory (under System Tables in your database if you're looking in SQL Server Management Studio). You'll need to wipe it out.

在第2阶段的好消息,反向工程数据库到code,是,微软已经发布了一个工具进入公测,这将做到这一点给你。

The good news on the second step, reverse engineering the db into code, is that Microsoft has released a tool into beta that will do this for you.

步行通过反向工程分贝,和EF电动工具

您可以跳过很多的第一步,因为有他们只是建立一个数据库,并增加了一些废话,这样他们可以证明你需要做的:反向工程分贝

You can skip many of the first steps there since they're just setting up a DB and adding some nonsense to it so they can demonstrate what you need to do: Reverse Engineer a db.

它也可以使用手动迁移到解决此方案是可行的。使数据库的备份,然后运行:

It can also be feasible to use a Manual Migration to work around this scenario. Make a backup of the db, then run:

add-migration WhateverYouWantToCallThis

的修改需要被运行会出现在生成的C#的命令的分贝EF迁移。现在,就看你与他们鼓捣既解决该问题,什么它试图做(例如试图删除已经被删除列),并落实到位的事情,它需要前进(例如加回一个表,你仍然有你的模型,但你在你的数据库手动删除)。

The modifications to the db EF Migrations that need to be run will appear in the generated C# commands. Now it's up to you to tinker with them to both work around the problems with what it's attempting to do (for example attempting to delete columns that have already been deleted), and put into place things it will need going forward (for example adding back a table you still have in your model but you manually deleted in your db).

一旦添加了这一点,并运行更新数据库-f ,EF code首先将只接受在信念,你已经更新了数据库的方式它需要,并更新其散列根据最终结果。如果你做出了正确的改变,你现在可以进行迁移是正常的。如果这样还是引起了错误,你通常可以复制手动迁移的命令某处,并将其删除,恢复从您的备份数据库,再添加一个手动迁移,然后再试一次。最坏的情况下,你再打上面的逆向工程的一步。

Once you've added this and run update-database -f, EF Code First will just accept on faith that you've updated the database the way it needs to be, and update its hash based on the end result. If you made the right changes you can now proceed with Migrations as normal. If this still causes errors you can usually copy the commands of the manual migration out somewhere and delete it, Restore the db from your backup, add a manual migration again and try again. Worst case you resort to the reverse engineering step above.

这篇关于实体框架code-第一:如何手动更新数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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