C# – Implementing Inheritance – ASP.NET MVC with EF Core

Slowly, after handling concurrency in the ASP.NET MVC application, we are approaching the end of the video tutorial series. Implementing inheritance is the prelast one and it is actually pretty an important one as well. In the video, the following points are mentioned:

  • Map inheritance to database
  • Create the Person class
  • Update Instructor and Student
  • Add Person to the model
  • Create and update migrations (although at the end the DB is deleted)
  • Test the implementation

The interesting part of the video, is how the previous two tables for Instructores and Estudiantes were “translated” to a single table “Person”, which combines them only because the 2 tables inherit from it:

And the data in the Personas table is actually a combination of both, with added column “Discriminator”, in order to refer to the class correctly (To be honest, I was expecting something quite different, but EF Core’s solution is good:

Probably it would be interesting to take a look once again of the rewriting of the Up method in the Migration, as far as it is needed, if you implement such a change in the middle of the project. If it is not implemented correctly, some DB data will be lost.

The GitHub project is here – https://github.com/Vitosh/ASP/tree/master/EFCoreAsp.NetMvcWebApp/ContosoUniversity009

Enjoy it!

Tagged with: , , ,