EF代码优先-如果模型更改,请重新创建数据库 [英] EF Code First - Recreate Database If Model Changes
问题描述
我目前正在一个项目中,该项目将EF Code First与POCO一起使用.到目前为止,我有5个POCO,具体取决于POCO用户".
I'm currently working on a project which is using EF Code First with POCOs. I have 5 POCOs that so far depends on the POCO "User".
POCO用户"应该引用我已经存在的MemberShip表"aspnet_Users"(我将其映射到DbContext的OnModelCreating方法中).
The POCO "User" should refer to my already existing MemberShip table "aspnet_Users" (which I map it to in the OnModelCreating method of the DbContext).
问题是我想利用Scott Gu在以下位置显示的如果模型更改就重新创建数据库"功能:
The problem is that I want to take advantage of the "Recreate Database If Model changes" feature as Scott Gu shows at: http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx - What the feature basically does is to recreate the database as soon as it sees any changes in my POCOs. What I want it to do is to Recreate the database but to somehow NOT delete the whole Database so that aspnet_Users is still alive. However it seems impossible as it either makes a whole new Database or replaces the current one with..
所以我的问题是:我注定要手动定义数据库表,还是可以以某种方式将POCO合并到当前数据库中,并且仍然可以使用该功能而不擦除所有内容?
So my question is: Am I doomed to define my database tables by hand, or can I somehow merge my POCOs into my current database and still take use of the feature without wipeing it all?
推荐答案
从CTP5中的EF Code First开始,这是不可能的. Code First将删除并创建您的数据库,或者根本不涉及数据库.我认为在您的情况下,您应该手动创建完整的数据库,然后尝试提出与数据库匹配的对象模型.
As of EF Code First in CTP5, this is not possible. Code First will drop and create your database or it does not touch it at all. I think in your case, you should manually create your full database and then try to come up with an object model that matches the DB.
也就是说,EF团队正在积极研究您要寻找的功能:更改数据库而不是重新创建数据库:
代码优先数据库演进(又名迁移)
That said, EF team is actively working on the feature that you are looking for: altering the database instead of recreating it:
Code First Database Evolution (aka Migrations)
这篇关于EF代码优先-如果模型更改,请重新创建数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!