如何从 EF 代码优先转向更传统的开发策略?

发布于 2024-11-03 16:32:08 字数 300 浏览 1 评论 0原文

我目前正在使用 EFCF(EF 代码优先)和 MVC3 开发一个小型应用程序。 EFCF 似乎非常适合开发原型或 v1 应用程序。 v1 版本发布后,我部署的数据库会发生什么情况?我将如何部署 v1++?特别是数据库。

使用 EFCF,我有 2 种可用模式:如果不同则重新创建或始终重新创建。没有“差异”模式可以保留我的数据并修补模型更改。我该怎么办呢?我想我可以将 v1 DB 与 v1++ DB 进行比较,然后走这条路线,或者我可以从 EFCF 迁移出来,采用更传统的路线:修改 DB,然后更新 POCO 以反映更改。问题是我不太理解后者,也无法制作合适的谷歌查询。

I am currently developing a small application with EFCF (EF code first) and MVC3. It seems that EFCF is great for developing a prototype or v1 application. What happens to my deployed databases after a v1 release? How would I go about deploying a v1++? Specifically the DB.

With EFCF I have 2 modes available, recreate if different or recreate always. There is no "diff" mode that retains my data and patches model changes. How would I go about this? I figure I could diff the v1 DB with the v1++ DB and go that route, or I could migrate away from EFCF and go to the more traditional route of modifying the DB then updating my POCOs to reflect the changes. The problem is I don't really understand the latter and have not been able to craft a suitable Google query.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

风柔一江水 2024-11-10 16:32:08

任何用于数据迁移的自动工具仍然不应该接触您的生产数据库。代码优先方法适用于开发。一旦将代码移至生产环境,就应该关闭数据库生成,并且在升级到新版本时应该为数据库创建更改脚本。您可以使用一些帮助工具为您创建 diff 脚本 - 例如 VS 数据库工具(VS 2010 Premium 或 Ultimate)或 Red Gate 工具。

Any automatic tool for data migration is still something that should never touch your production database. Code-first approach is for development. You should turn off database generation once you move your code to the production and you should create change script for database when you upgrading to a new version. You can use some help tools to create diff script for you - like VS Database tools (VS 2010 Premium or Ultimate) or Red Gate tools.

执手闯天涯 2024-11-10 16:32:08

2 个链接:


  1. 数据库
  2. 另一篇文章有​​更多内容
    详细信息

此方法从数据库模式生成所有类。在这里您可以自由更改数据库架构。您唯一需要做的就是更新实体数据模型,但这不会影响您的数据,也不应该破坏您的代码,除非您要删除字段。

2 links:

  1. Creating the models from the
    database
  2. Another article with more
    details

This method generates all the classes from the database schema. Here you are free to change your database schema. The only thing that you will have to do is update your entity data model but that doesn't affect your data nor should it break your code unless you are removing fields.

霓裳挽歌倾城醉 2024-11-10 16:32:08

看来 EFCF 非常适合开发原型或 v1 应用程序。

Bingo。目前,EF4.1 不具备任何真正的版本控制功能,尽管他们正在致力于“迁移”和“架构演化”。 此处查看“不支持的内容”

就我个人而言,我真的很喜欢代码映射,因此我的建议是,一旦您的初始模式就位,您可以通过以下方式进行未来的更改:(a) 修改您的数据库,(b) 修改您的 POCO,然后 (c) 修改您的代码-第一映射。当然,您会告诉您的数据库对象永远不要删除数据库。

对于大的模式更改来说,这可能很容易出错,但现在必须这样做。

It seems that EFCF is great for developing a prototype or v1 application.

Bingo. Right now, EF4.1 doesn't t have any real versioning capabilities, although they are working on "migrations" and "schema evolution". Check out "What's Not Supported" here.

Personally, I really like code mapping, and so my recommendation is that once your initial schema is in place, that you make future changes by (a) modifying your database, (b) modifying your POCO's, and then (c) modifying your code-first mappings. You would, of course, tell your Database object never to drop your database.

This is can be error prone for big schema changes, but it will have to do for now.

半城柳色半声笛 2024-11-10 16:32:08

我认为您需要迁移 EFCF 功能。

在更改模型时不丢失数据的最简单方法是启用允许数据丢失的自动迁移:

internal sealed class Configuration : DbMigrationsConfiguration<YourFancyDBContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
        AutomaticMigrationDataLossAllowed = true;
    }

...

Database.SetInitializer(new MigrateDatabaseToLatestVersion<YourFancyDBContext, Configuration>());

实际上,首先需要执行几个步骤:http://msdn.microsoft.com/en-US/data/jj591621

I think you need the migrations EFCF feature.

The simplest way of not losing your data while changing models is enabling auto migrations with data loss allowed:

internal sealed class Configuration : DbMigrationsConfiguration<YourFancyDBContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
        AutomaticMigrationDataLossAllowed = true;
    }

...

Database.SetInitializer(new MigrateDatabaseToLatestVersion<YourFancyDBContext, Configuration>());

Actually, it requires a few steps first: http://msdn.microsoft.com/en-US/data/jj591621

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文