From the course: Using Entity Framework Core with Legacy Databases

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Creating an inheritance relationship

Creating an inheritance relationship

From the course: Using Entity Framework Core with Legacy Databases

Start my 1-month free trial

Creating an inheritance relationship

- [Instructor] Sometimes it makes sense to consider a table to represent more than one type. Often in existing databases, there's a column, or several columns, that are only applicable for some of the rows. We can use an inheritance relationship in code to segment those columns and have separate types holding these fields. This is called table per hierarchy relational mapping. Inheritance won't be set up by the scaffolding process. We have to manually set it up. Let's navigate to the Product model. We can see on our Product model the Perishable property. And we see these two columns, ExpirationDays and Refrigerated. These columns only apply if a product is perishable. So let's separate perishable products from regular products. We'll start by copying the Product model and renaming it to PerishableProduct. Let's copy that so we can paste it a couple more places. In the PerishableProduct file, we'll replace Product with PerishableProduct. PerishableProduct will extend Product. We can…

Contents