Summary

We started this chapter by enumerating a number of things we couldn’t implement due to our Dummy Data Provider limitations; in order to overcome these, we chose to replace it with a real Data Provider built upon a persistent Database.

Entity Framework Core seemed an obvious choice to get what we want, so we added its relevant packages to our project; we briefly enumerated the available Data Modeling approaches and resorted to using Code-First due to its flexibility.

Right after that, we proceeded to create our entity classes--ApplicationUser, Quiz, Question, Answer and Result--along with a set of relationships taking advantage of the renowned Entity Framework Core’s Convention over Configuration approach. Then, we built our ApplicationDbContext class accordingly.

After completing our Data Model, we chose the Database Engine, quickly resorting to the Visual Studio's default MSSQL LocalDb instance; we added the connection string to the appsettings.json file and passed it to ApplicationDbContext through the Setup class. Doing this allowed us to add our first migration using the dotnet ef tool using PowerShell and, once done, use Code-First to generate our Database accordingly.

We didn’t want to leave our Database empty, so we implemented a DbSeeder static class to seed it with some sample data; we learned how our ApplicationDbContext instance can be used within a static class, thanks to the ASP.NET Core Dependency Injection approach; doing that took a reasonable, yet well spent, amount of time.

Finally, we switched back to the QuizController class and updated it to make it use the new Data Provider, replacing the outdated one; in order to do this in the best possible way, we also installed Mapster, a fast and easy-to-use ORM mapping tool that we used to successfully perform some Model-to-ViewModel mapping tasks.

After completing all these tasks, we ran our application in Debug mode to verify that everything was still working as intended. The HomeView is very similar to the one we had at the end of Chapter 3, Frontend with Angular, yet a lot of things changed on the backend. Our application is now ready to grow the way we want it to.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.217.198