Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Don't understand DbContext -- per application or per model

What is the idea behind a DbContext class (EF)? Should I have one per model, one per application or maybe something else?

I guess I don't understand what they're used for. I get that I can put my data fetching logic in there, and that's great, but in that case, it seems to me that I should have one per model. Although if I have one per model, VS will create one connection string for each DbContext, ie. for each model. I'm surely missing something here.

like image 589
Eduard Luca Avatar asked Jan 12 '23 14:01

Eduard Luca


1 Answers

It's the "connection to the database" so to speak, and you would generally have one for each database you connect to. In general this is one per application. See, having a list of models is great, but you have to yet still have a connection to the actual database to get the data into those models.

like image 97
Mike Perrenoud Avatar answered Feb 02 '23 06:02

Mike Perrenoud