Entity Framework Core 2.2:禁用特定实体的迁移

编程入门 行业动态 更新时间:2024-10-24 14:26:58
本文介绍了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:禁用特定实体的迁移

本文发布于:2023-11-14 00:52:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585772.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   Framework   Entity   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!