The Entity Framework – Part 2 – Inheritance

As promised on my previous Entity Framework-related post I will try through a series of posts which will contain examples, to talk about the ADO. NET Entity Framework and all the cool features it provides the developer with. This post discusses Inheritance in ADO .NET Entity Framework.

Inheritance allows creating entities having as base type other entities, thus allowing constructing even more sophisticated data models with ease.

 


Learn all about Entity Framework.
Enroll to our online course “Entity Framework: Getting Started – Complete Beginners Guide

Learn all about Entity Framework, what it is, how it works, how to use it for data access when developing .NET apps, what are its available workflows and their pros and cons. Also, via comprehensive live demonstrations, we will build many apps using Visual Studio with C# and Entity Framework.

Entity Framework: Getting Started (Ultimate Beginners Guide) - Online course
Lifetime Access / Downloadable Resources and more!

Learn More


The introductory post provided information on how you can create an entity data model by connecting to a SQL Server 2008 database.

 

About Inheritance in Entity Framework

For better explaining inheritance in this post, I edited the authors table on the pubs database and added another column named type. Then I updated the records and populated this new column with data as on the below screenshot:

Inheritance in Entity Framework

So, you can see some authors having values (SF or CM) in the type column. This column presents the type of publications some authors create. SF stands for Science Fiction and CM for something else (I forgot what to be honest :). Authors having no type (that is, NULL) means that they create publications not belonging to a specific genre/type all the time.

Let’s start!

First you have to create a Windows Forms C# Application, then right click on the project and add as a new item an ADO .NET Entity Data Model. After taking care of the database connection for this model (see introductory post for more information), the next step is to add the authors table in the entity data model designer:

So on the above screenshot you can see that the authors entity has been created and mapped to the corresponding table on the pubs database. In order to use inheritance, you have to create a new entity (or more than one) that have as a base type the original entity; in this case the authors entity. To this end, I have created the entity AuthorsWithType:

Inheritance in Entity Framework

Now you can see that on the entity data model designer workspace there are two entities; the base entity (authors) and the inherited entity (AuthorsWithType):

Inheritance in Entity Framework

At this point, you have to cut-paste the connecting propery (in this case type) from the base entity, that is authors, to the inherited entity – AuthorsWithType. Also, the authors entity has to be set as abstract (entity properties) as it won’t be used for data manipulation because we are going to use the AuthorsWithType for this purpose.

The next screenshot displays the mapping editor tool and shows how the type column in the authors table has to be mapped to the type scalar property (string) in the AuthorsWithTypes entity. Also at this point, you also have to add a condition as shown below:

Inheritance in Entity Framework

The purpose of the inherited entity is to list only the authors having a specific type/genre of publications and that is why we added the above condition.

You then have to save the data model and build the solution.

Then we start the process of adding a new datasource to our project of the type object:

Inheritance in Entity Framework

We then bind our datasource to the AuthorsWithType object:

Inheritance in Entity Framework

We drag-and-drop our new datasource on our application’s main form and we get the following datagrid:

Inheritance in Entity Framework

We then enable the Save icon (for being able to add some code later for saving any changes we perform on the datagrid contents during runtime):

Inheritance in Entity Framework

Then we add the following code:

Inheritance in Entity Framework

As the code comments say, we have to: (i) create a pubsEntities object (this is the entity data model connection string), (ii) instantiate the object, (iii) navigate through the entity data model to the authors data of type AuthorsWithType (this is a way of using inheritance via source code), and (iv) add a single line of code for allowing when clicking on the Save button to post any changes performed on the data during runtime (before that you have to double-click on the Save button during design-mode in order to generate the handling method called: authorsWithTypeBindingNavigatorSaveItem_Click).

At this point I would like to comment a little bit Line 3. Line 3 uses the OfType keyword for declaring the datasource. When using the OfType keyword, you are able to browse to the inherited entities in the entity data model via their base type entities. In this example, you browse the AuthorsWithType via its base type, that is authors.

When running the program, you will get the form with only displaying authors that have a type (SF or CM):

Inheritance in Entity Framework

Now, let’s change something on the datagrid contents. So let’s change the au_lname of the first record from White to White-TEST and click on the save button:

Inheritance in Entity Framework

Finally close and re-run the application; well, that’s it, you can see that the above modification was saved on the database:

The command model.SaveChanges() posted the changes first through AuthorsWithType entity, then authors and finally on the authors table on the pubs database. A simple command did all these! 🙂

I hope you found this post useful. The Next post in the Entity Framework series will be dedicated to Entity Splitting.

 

Watch our video: What is Entity Framework and what are its Benefits?

 

Watch our video about Inheritance in ADO .NET Entity Framework!

 

Watch our video: Entity Framework: Getting Started – Complete Beginners Guide (Course Preview)

Enroll to the Course!

 


Upgrade your Tech Skills – Learn all about Azure SQL Database

Enroll to our online course on Udemy titled “Introduction to Azure SQL Database for Beginners” and get lifetime access to high-quality lessons and hands-on guides about all aspects of Azure SQL Database.

Introduction to Azure SQL Database (Online Course - Lifetime Access)
(Lifetime Access/ Live Demos / Downloadable Resources and more!)

Learn More

 

Featured Online Courses:

 

Check our other related Entity Framework and .NET articles:

 

Read also:

 

Subscribe to our newsletter and stay up to date!

Subscribe to our YouTube channel (SQLNetHubTV)!

Like our Facebook Page!

Check our SQL Server Administration articles.

Check out our latest software releases!

Check our eBooks!

 

Rate this article: 1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)

Loading...

Reference: SQLNetHub.com (https://www.sqlnethub.com)

© SQLNetHub