C
C#2y ago
KooriByte

✅ Where to declare relationships when you have two DBContexts?

Hi! I ended up with two DBContexts in my project (ApplicationDbContext and an IdentityDbContext)... Now I have to do a many to many relationship between an object and a user, but I'm not sure how would this work. I declared the relationship in one DbContext, and the migration with this one worked (with a warning that I might lose data). If I try to do a Migration with the other Context, it screams to me an error that i know how to fix, but would imply in declaring this relationship two times, and I'm not sure if this is ok. Has anyone here experienced this?? I would really appreciate some help, it is the first time I try to develop something without a tutorial and it is a bit overwhelming at times. Tkss 😄
24 Replies
Angius
Angius2y ago
Don't have two DbContexts
KooriByte
KooriByteOP2y ago
at this point in my project, I'm not sure how to delete this second one and use only one
Angius
Angius2y ago
What do you have in the IdentityDbContext? Or did you not extend it and are just using it as-is? If the latter, simply make your "main" context inherit from it ApplicationDbContext : IdentityDbContext
KooriByte
KooriByteOP2y ago
nothing changed on IdentityDbContext
Angius
Angius2y ago
Then nothing stops you from having a single context that inherits from identity context
KooriByte
KooriByteOP2y ago
is it ok to change "public class ApplicationDBContext : DbContext" to "ApplicationDbContext : IdentityDbContext"
Angius
Angius2y ago
Yeah
KooriByte
KooriByteOP2y ago
oh, nice
Angius
Angius2y ago
IdentityDbContext inherits DbContext and adds all the users, roles, claims, etc.
KooriByte
KooriByteOP2y ago
it was way simpler than what I expected
Angius
Angius2y ago
So it's safe to just inherit from it
KooriByte
KooriByteOP2y ago
I'll try it brb
KooriByte
KooriByteOP2y ago
KooriByte
KooriByteOP2y ago
After this, it is ok to just delete the IdentityDbContext? and keep just the Application one
Angius
Angius2y ago
Yeah
Angius
Angius2y ago
And you can shorten this with a using btw
KooriByte
KooriByteOP2y ago
i was using this, problem was I caller my own IdentyDbContext "IdentityDbContext" and it asked me to specify wich one I'm trying to do the migration now, some minor errors, but I think I can fix it do I need to delete previous migrations?
Angius
Angius2y ago
I don't think so Although with a change this big, it might be prudent to drop the database, delete migrations, and start anew
KooriByte
KooriByteOP2y ago
I droped the db, but didnt deleted the migrations I'm afraid to delete something that I shouldn't lol
Angius
Angius2y ago
Migrations are safe to delete Database schema is kept in your models and config
KooriByte
KooriByteOP2y ago
what are those ContextModelSnapshots for? deleted migrations and worked 😄 Do I need to close this post or do I leave it open as it might be useful for someone in the future?
Angius
Angius2y ago
Use /close or $close, so that the bot changes the title and all. The post will remain up, no worries
KooriByte
KooriByteOP2y ago
oh, ok @Angius Thank you very much for the help, you really saved me here
Angius
Angius2y ago
Anytime 👌
Want results from more Discord servers?
Add your server