代码优先迁移动态组合模型 [英] Code First Migrations for dynamically assembled model

查看:147
本文介绍了代码优先迁移动态组合模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库模型(有时称为 context )根据安装的服务和/或插件在启动时动态组合。插件和服务通过我的IoC容器导出他们的模型定义片段,并且应用程序核心选择它们并在调用 DbContext.OnModelCreating 方法时运行它们。

My database model (sometimes referred to as "context") is dynamically assembled at startup based on which services and/or plugins are installed. Plugins and services export their model definition fragments through my IoC container and the application core picks them up and runs them when the DbContext.OnModelCreating method is called.

问题是:我可以(和我如何)使用此设置进行代码优先迁移

(以下是更多关于什么的信息我已经尝试了,有什么特别的问题)

The question is: Can I (and how do I) use Code First Migrations with this setup?
(below is more information on what I've tried and what particular problems are)

在我以前的项目中,数据库是继承自一些旧的代码,所以我们不能使用任何Code First数据库无论如何我们只是保留了一长串的delta脚本,并在部署时手动执行(这是一个单一主机类型的项目)。

In my previous project, the database was inherited from some old code so we couldn't use any of the Code First database generation stuff anyway. We simply kept a long line of delta scripts and executed them manually on deploy (it was a single-host kind of project).

现在我开始一个新的项目,而这一次,数据库是全新的,准备为Code First玩。最初,我对Code First Migrations都感到兴奋,好像是要走的路。直到我真的尝试了由于在我的项目中没有明确定义的 DbContext ,初始尝试失败。

Now I'm starting a new project, and this time, the database is brand new, ready for Code First to play with. Initially, I was all excited about Code First Migrations, seemed like the way to go. Until I actually tried it. The initial attempt, quite obviously, failed due to the absence of an explicitly defined DbContext in my project.

所以远远看来,唯一可行的选择是手动代码迁移,我完全可以使用它。然而,事实证明,这并不像创建从 DbMigration 继承的几个类。

So far, it looks like the only viable option is to manually code migrations, with which I am perfectly fine. However, it turns out that this is not as simple as just creating a few classes inherited from DbMigration.

之后对一个小型测试项目进行了一些实验,我发现迁移自动生成器添加了一个 IMigrationMetadata 的实现,其中包含我的模型的哈希值Source 和 Target 属性的值。据推测,这个哈希然后用于标识跨数据库(如 __ MigrationHistory 表中记录的)的当前状态迁移到最新状态的路径,如代码中的模型。这完全是有道理的,但... ...

After some experimentation on a small test project, I was able to find out that the migration autogenerator adds an implementation of IMigrationMetadata, which, among other things, contains a hash of my model as the values of the Source and Target properties. Presumably, this hash is then used to identify a path across migrations from the "current" state of the database (as recorded in the __MigrationHistory table) to the newest state as defined by the model in code. This totally makes sense, but...

自然,我不知道在哪里可以得到我的模型的哈希,这使我无法实现我的迁移中的IMigrationMetadata

Naturally, I have no idea where to get that hash for my model, which makes me unable to implement IMigrationMetadata on my migrations.

另一方面,我看到元数据接口不包含在 DbMigration 类本身,这让我觉得它可能是可选的。然后,迁移实际上可以在没有哈希值的情况下工作,但问题是 - 如何?

On the other hand, I see that the metadata interface is not included in the DbMigration class itself, which makes me think that it might be optional. It then follows that migrations can actually work without the hash values, but the question is - how?

我可以在互联网上找到的所有信息都是简单的,非常基本的教程。没有关于如何手动创建迁移(以及是否支持)的信息。没有关于它如何实际工作以及如何扩展它的文档。而且从外部来看并不是很明显。

我准备在这一点上诉诸于ILSpy,但整个EF非常复杂,恐怕在合理的时间内我可能找不到我需要的东西。

All the information I could find on the internet is just simple, very basic tutorials. No information on how to create migrations manually (and whether it's even supported). No documentation on how it actually works and how to extend it. And it is not quite obvious from outside.
I am ready to resort to ILSpy at this point, but the whole EF is so complex that I fear I may not be able to find what I need in reasonable time.

推荐答案

以下是几个想法,您可以一起来找到适合您的解决方案。我意识到我在其他线程中提到了其中一些内容,但是我在这里提供了这些信息给他人阅读这个问题。

Here are a few ideas that you could pull together to find a solution that works for you. I realize I mentioned some of these in our other thread, but I'm including them here for others reading this question.


  • 自动迁移允许Code First自动计算并对数据库应用更改。

  • 您可以编写自己的代码来生成和应用迁移。我已经写了一个关于应用迁移的帖子 MigrationsScaffolder 类将帮助您创建迁移。

  • Automatic migrations allow Code First to automatically calculate and apply changes to the database.
  • You can write your own code to generate and apply migrations. I've written a post about applying migrations and the MigrationsScaffolder class will help you create migrations.

这篇关于代码优先迁移动态组合模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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