0

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.

Kamran Asim
  • 670
  • 4
  • 16
  • Which store are you using? If SQL Server or similar you can create a DB link between the two and setup a view so you can actually map the object to your context. – Canica Sep 24 '19 at 20:12
  • possible duplicate: https://stackoverflow.com/questions/22038924/how-to-exclude-one-table-from-automatic-code-first-migrations-in-the-entity-fram – fenixil Sep 25 '19 at 03:30
  • Thanks @Fenixil. I checked the suggested solution, but it didn't worked for me. i.e. Ignore will also ignore the foreign key and the second suggestion to disable migration for a while is not answer in my case – Kamran Asim Sep 25 '19 at 05:03
  • Thank @jcruz. I am using SQL Server. Can you please explain how DB link between DB context will help me. I never applied DB link in EF. let me check if it can solve my issue – Kamran Asim Sep 25 '19 at 05:06

1 Answers1

0

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.

dropoutcoder
  • 2,627
  • 2
  • 14
  • 32