简体   繁体   English

实体框架核心multipleDbContext代码优先迁移共享实体

[英]Entity framework core multipleDbContext code-first migration of shared entity

I am using mutiple dbContext (IdentityDbContext and DBContext) in my app.我在我的应用程序中使用了多个 dbContext(IdentityDbContext 和 DBContext)。 Due to some reasons, I am bound to use multiple DBContext, as I know you will answer me to Use only DBContext as inherit DBContext from IdentityDbContext (I will not do this).由于某些原因,我一定会使用多个 DBContext,因为我知道您会回答我只使用 DBContext 作为从 IdentityDbContext 继承 DBContext(我不会这样做)。

Anyhow, I have a table named Patient in DBContext, which has Foreign key relation with User.无论如何,我在 DBContext 中有一个名为 Patient 的表,它与 User 具有外键关系。 and User belongs to IdentityDBContext.并且用户属于 IdentityDBContext。

Now when I am generating Migration scripts from DBContext, It adds DDL for User;现在,当我从 DBContext 生成迁移脚本时,它为用户添加了 DDL; Although User table is already created during IdentityDBContext migration.虽然在 IdentityDBContext 迁移期间已经创建了 User 表。 It seems, there is something missing in migration relation.看来,迁移关系中缺少一些东西。 I want to create foreign key Patient.UserId ==> User.UserId, but don't create User table ddl scripts.我想创建外键 Patient.UserId ==> User.UserId,但不要创建用户表 ddl 脚本。

Can anyone help me how to define such relationship between two entities that are defined in another DBContext.谁能帮助我如何定义在另一个 DBContext 中定义的两个实体之间的这种关系。

The workaround here is to create migration and comment generated code(similar to previous EF version -IgnoreChanges switch).这里的解决方法是创建迁移和注释生成的代码(类似于以前的 EF 版本 -IgnoreChanges 开关)。 Apply migration that in the end changes nothing, but keeps track of current DbContext model snapshot and will never generate code again.应用最终不会改变任何内容的迁移,但会跟踪当前的DbContext model 快照,并且永远不会再次生成代码。

The downside is that you need to do such a step everytime you change IdentityDbContext and apply new migration.缺点是每次更改IdentityDbContext并应用新迁移时都需要执行此步骤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM