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

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

问题描述

在实体框架6中,您可以为DbContext指定一个ContextKey,以便在为多个DbContexts使用一个数据库时,Migrations表了解了什么迁移目标是什么DbContext。



最近几个小时,我花了几个小时尝试在实体框架核心中实现同样的功能,但没有实现,文档不足。



EF Core中的新Migrations表只有两列 - MigrationId,ProductVersion。



我以为HasDefaultSchema是答案,但似乎不是。



想法?

解决方案

以前,Entity Framework在数据库中存储了模型的快照。这导致Entity Framework必须每次都查询数据库,以便确定已经运行的迁移。



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



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



可爱。


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.

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.

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

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

Ideas?

解决方案

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.

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

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.

Lovely.

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

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