C# – Get started with EF Core in an ASP.NET MVC Web App – Video

Lately, I have started with creating video tutorials for ASP.NET. Thus, today it is time to deep dive in the ASP.NET once again, following the Microsoft tutorial for ASP.NET MVC Web App from here.

In the video below, the following points are presented:

  • Create an ASP.NET Core MVC web app
  • Set up the site style
  • Learn about EF Core NuGet packages
  • Create the data model
  • Create the database context
  • Register the context for dependency injection
  • Initialize the database with test data
  • Create a controller and views
  • View the database

In the video, there were 2 intesting moments, when the DB was not initialized correctly – once because of a missing line in the Startup.cs file and one, because the AutoId of one of the classes was overridden without informing the Entity Framework (EF) properly.

This is how the SchoolContext.cs  class informs the EF how to name specific tables in the database upon creation:

And this is how the Main  method is modified:

Thus, it does the following on startup:

  • Gets the db context from the dependency injection container;
  • Seeds the data from the static DbInitializer.Initialize(context)  method;
  • Disposes correctly the cotext, after the seed method is ready, with the “magic” of using();

The rest of the code is in GitHub – https://github.com/Vitosh/ASP/tree/master/EFCoreAsp.NetMvcWebApp/ContosoUniversity001

Enjoy!

Tagged with: , , , , , ,