Entity Framework Core 2.2:禁用特定实体的迁移 [英] Entity Framework Core 2.2 : Disable migrations for specific entities

查看:82
本文介绍了Entity Framework Core 2.2:禁用特定实体的迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在已经创建数据库的现有系统上构建一个aspnetcore应用程序,然后在其上添加一些表。

I'm trying to build an aspnetcore application over an existing system where the database is already created, and I'm going to add some tables over it.

我对数据库进行了反向工程,以将现有表作为实体添加到我的应用程序中,并且编写了自己的实体,稍后将添加它们。最后,所有实体都添加到单个DbContext中。

I've reverse engineered the database to add the existing tables as entities to my application, and I've written my own entities that will be added later. Finally, all the entities are added to a single DbContext.

我的要求如下:


  • 我想为新添加的实体启用代码优先迁移

  • 我不希望对现有表/实体进行迁移

注意:我不希望触摸生成的迁移代码。

Note: I prefer not to touch the generated migration code.

是否有适当的方法

推荐答案


是否有正确的方法来处理这种情况?

Is there a proper way to handle this scenario?

编辑迁移代码是处理这种情况的正确方法。

Editing the Migration Code is a proper way to handle this scenario.

可以使用迁移创建一个DbContext,该迁移仅包含映射到要使用迁移管理的表的实体。然后,使用所有用于读取和写入数据库的实体创建另一个DbContext。

Alternatively, you could create one DbContext with Migrations containing only the Entities mapped to Tables you want to manage using Migrations. Then create another DbContext with all the Entities which is used to read and write to the database.

请注意,您的迁移上下文可能缺少导航属性,如果您不想添加 real 外文,则仅包含相应的外键属性数据库中的键引用不受迁移控制的表。

Note here that your Migration Context may lack Navigation Properties, containing only the corresponding Foreign Key Properties if you don't want to add real Foreign Keys in the database refering to tables not controlled by your Migrations.

这篇关于Entity Framework Core 2.2:禁用特定实体的迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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