✅ 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
Don't have two DbContexts
at this point in my project, I'm not sure how to delete this second one and use only one
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
nothing changed on IdentityDbContext
Then nothing stops you from having a single context that inherits from identity context
is it ok to change "public class ApplicationDBContext : DbContext" to "ApplicationDbContext : IdentityDbContext"
Yeah
oh, nice
IdentityDbContext
inherits DbContext
and adds all the users, roles, claims, etc.it was way simpler than what I expected
So it's safe to just inherit from it
I'll try it
brb
After this, it is ok to just delete the IdentityDbContext?
and keep just the Application one
Yeah
And you can shorten this with a
using
btwi 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?
I don't think so
Although with a change this big, it might be prudent to drop the database, delete migrations, and start anew
I droped the db, but didnt deleted the migrations
I'm afraid to delete something that I shouldn't lol
Migrations are safe to delete
Database schema is kept in your models and config
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?
Use
/close
or $close
, so that the bot changes the title and all. The post will remain up, no worriesoh, ok
@Angius Thank you very much for the help, you really saved me here
Anytime 👌