Using ApplicationDbContext in Repository Pattern

I want to implement repository pattern in my ASP.NET MVC project. I’ve seen a lot of implementations when searching google and I’m a little bit confused. Most of them created their own Context class which inherited from DBContext class and then injected it in a repository (or repositories) constructor.
None of the articles I found explain why the custom DBContext class is created and why they just don’t use default ApplicationDbContext and HOW to change your app code to adapt to the new Context class. They don’t even mention the class.

I’d rather use default ApplicationDbContext which I would inject in repository classes, because at the moment I don’t see a point in creating a new DBContext class. Am I missing something? Is it a bad practice?

If, for some reason, I will have to add new DbContext class, how to edit my code (web.config and others) to adapt it? If I already made migrations to database, would change of DBContext class affect the database? Would it be necessary to do migrations once again?

To give you an example look at the MSDN documentation:
https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application
I just have no idea why the author uses SchoolContext instead of ApplicationDbContext.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

it’s a broad question, a guess that’s the reason for the down votes.

You do seem confused. First off, I’m not an expert (see points). I did get a little bit in the weeds with the default ApplicationDbContext. Maybe you use a template where it pops up? Anyway, I launched the MVC-template and tehre’s no default context class provided.

In short you create your own Context-class. You give it any name you want, given that in ends with Context AND IT INHERITS from DbContext. If not the Entity framework will not recognise it, and will not know what to do with it when building the database an so on. See your article:

The main class that coordinates Entity Framework functionality for a given data model is the database context class. You create this class by deriving from the System.Data.Entity.DbContext class. In your code you specify which entities are included in the data model. You can also customize certain Entity Framework behavior. In this project, the class is named SchoolContext.
Blockquote

By default you have only one Context-class.

It is self evident you need to create your own context-class, since you can’t access the DbContext-class. (And even if you could, you should not want to do that, you need to separate the abstract and the implemented.)

I don’t understand §2. ApplicationDbContext is just a way to write MySelfNamedContextClass.

§3: I don’t know if working with two contect-classes is possible/feasible, but the simple implication is that you would be working with two separate databases. That’s already pretty complicated, and what you’re wrtiting about is not heading in that direction.

If I already made migrations to database, would change of DBContext
class affect the database? Would it be necessary to do migrations once
again?

Yes, absolutely it would. Your adding a table or a field or a constraint or a relationship to it … .
You can do two things depending on the changes you bring:
(i) add a supplemental migration
(ii) You would have to rewind your migrations (remove-migration). It could be that you need to reset your db to your first migration (update-database ‘here name of first migration without the quotation marks’. If you get stuck with that, and you’re still on local db, just remove db by deleting it outright (maybe after closing solution) and rewind and rebuild the migrations and rebuild the database.
Especially for scenario (ii), if you have already data in the database (that isn’t seeded into it), you would need to take measures to store it somewhere !!!

I hope this somehow answers your questions.

Kind regards.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x