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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.DropForeignKey( name: "FK_Enrollment_Student_StudentID", table: "Enrollment"); migrationBuilder.DropIndex(name: "IX_Enrollment_StudentID", table: "Enrollment"); migrationBuilder.RenameTable(name: "Instructor", newName: "Person"); migrationBuilder.AddColumn<DateTime>(name: "EnrollmentDate", table: "Person", nullable: true); migrationBuilder.AddColumn<string>(name: "Discriminator", table: "Person", nullable: false, maxLength: 128, defaultValue: "Instructor"); migrationBuilder.AlterColumn<DateTime>(name: "HireDate", table: "Person", nullable: true); migrationBuilder.AddColumn<int>(name: "OldId", table: "Person", nullable: true); // Copy existing Student data into new Person table. migrationBuilder.Sql("INSERT INTO dbo.Person (LastName, FirstName, HireDate, EnrollmentDate, Discriminator, OldId) SELECT LastName, FirstName, null AS HireDate, EnrollmentDate, 'Student' AS Discriminator, ID AS OldId FROM dbo.Student"); // Fix up existing relationships to match new PK's. migrationBuilder.Sql("UPDATE dbo.Enrollment SET StudentId = (SELECT ID FROM dbo.Person WHERE OldId = Enrollment.StudentId AND Discriminator = 'Student')"); // Remove temporary key migrationBuilder.DropColumn(name: "OldID", table: "Person"); migrationBuilder.DropTable( name: "Student"); migrationBuilder.CreateIndex( name: "IX_Enrollment_StudentID", table: "Enrollment", column: "StudentID"); migrationBuilder.AddForeignKey( name: "FK_Enrollment_Person_StudentID", table: "Enrollment", column: "StudentID", principalTable: "Person", principalColumn: "ID", onDelete: ReferentialAction.Cascade); } |
The GitHub project is here – https://github.com/Vitosh/ASP/tree/master/EFCoreAsp.NetMvcWebApp/ContosoUniversity009
Enjoy it!