同一数据库中的实体框架核心多 DbContext 迁移 [英] Entity Framework Core Mutiple DbContext Migrations in same database

查看:23
本文介绍了同一数据库中的实体框架核心多 DbContext 迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在实体框架 6 中,您可以为 DbContext 指定一个 ContextKey,以便在将一个数据库用于多个 DbContext 时,Migrations 表知道什么迁移针对什么 DbContext.

In Entity Framework 6 you could specify a ContextKey for a DbContext so that when using one database for multiple DbContexts the Migrations table was aware of what migration targeted what DbContext.

过去几个小时我一直在努力研究如何在 Entity Framework Core 中实现相同的目标,但尚未解决,并且缺少文档.

I've spent the last few hours trying to work out how you achieve the same in Entity Framework Core but haven't worked it out and documentation is lacking.

EF Core 中新的 Migrations 表只有两列 - MigrationId、ProductVersion.

The new Migrations table in EF Core only has two columns - MigrationId, ProductVersion.

我认为 HasDefaultSchema 是答案,但似乎不是.

I thought HasDefaultSchema was the answer but doesn't seem to be.

想法?

推荐答案

以前,Entity Framework 在数据库中存储模型的快照.这导致实体框架每次都必须查询数据库才能确定已经运行的迁移.

Previously, Entity Framework stored a snapshot of the model in the database. This resulted in Entity Framework having to query the database each time in order to work out what migrations had already run.

在 EF7 中,迁移历史记录存储在代码快照中(以及存储在熟悉的 Migrations 文件夹中的现有迁移).

In EF7 migration history is stored in a snapshot in code (along with the existing migrations stored in the familiar Migrations folder).

每次更新模型并创建迁移时,快照文件都会更新.因此,不需要在早期版本的实体框架中使用的 ContextKey.

Each time you update the model and create a migration the snapshot file gets updated. Therefore, there is no need for the ContextKey which was used in earlier version of Entity Framework.

可爱.

这篇关于同一数据库中的实体框架核心多 DbContext 迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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