简体   繁体   中英

Entity framework core multipleDbContext code-first migration of shared entity

I am using mutiple dbContext (IdentityDbContext and DBContext) in my app. 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).

Anyhow, I have a table named Patient in DBContext, which has Foreign key relation with User. and User belongs to IdentityDBContext.

Now when I am generating Migration scripts from DBContext, It adds DDL for User; Although User table is already created during IdentityDBContext migration. 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.

Can anyone help me how to define such relationship between two entities that are defined in another DBContext.

The workaround here is to create migration and comment generated code(similar to previous EF version -IgnoreChanges switch). Apply migration that in the end changes nothing, but keeps track of current DbContext model snapshot and will never generate code again.

The downside is that you need to do such a step everytime you change IdentityDbContext and apply new migration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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