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.
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:
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:
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):
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:
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:
We then bind our datasource to the AuthorsWithType object:
We drag-and-drop our new datasource on our application’s main form and we get the following datagrid:
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):
Then we add the following code:
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):
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:
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.
Featured Online Courses:
- Entity Framework: Getting Started – Complete Beginners Guide
- Introduction to Azure Database for MySQL
- Working with Python on Windows and SQL Server Databases
- Boost SQL Server Database Performance with In-Memory OLTP
- Introduction to Azure SQL Database for Beginners
- Essential SQL Server Administration Tips
- SQL Server Fundamentals – SQL Database for Beginners
- Essential SQL Server Development Tips for SQL Developers
- Introduction to Computer Programming for Beginners
- .NET Programming for Beginners – Windows Forms with C#
- Introduction to SQL Server Machine Learning Services
- SQL Server 2019: What’s New – New and Enhanced Features
- How to Import and Export Data in SQL Server Databases
- Learn How to Install and Start Using SQL Server in 30 Mins
- A Guide on How to Start and Monetize a Successful Blog
Check our other related Entity Framework and .NET articles:
- Entity Framework: Getting Started (Complete Beginners Guide – Online Course)
- The ADO .NET Entity Framework
- The Entity Framework – Part 1 – Introduction
- What Entity Framework Core is and How to Enable it in your C# Projects in Visual Studio
- Working with Entity Framework 6.3 and .NET Core 3.0
- Our Online Course – Entity Framework: Getting Started (Ultimate Beginners Guide)
- Screencast: How to Create an Entity Data Model
- Screencast: Using Inheritance in the ADO .NET Entity Framework
- Entity Framework: Code First
- The Net.Tcp Port Sharing Service service on Local Computer started and then stopped
- Using the C# SqlParameter Object for Writing More Secure Code
- Cannot declare instance members in a static class
- Cannot implicitly convert type ‘string’ to ‘System.Windows.Forms.DataGridViewTextBoxColumn
- Microsoft SSIS Service failed to start. Configuration system failed to initialize
- …more
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!
Artemakis Artemiou, a distinguished Senior Database and Software Architect, brings over 20 years of expertise to the IT industry. A Certified Database, Cloud, and AI professional, he earned the Microsoft Data Platform MVP title for nine consecutive years (2009-2018). As the founder of SQLNetHub and GnoelixiAI Hub, Artemakis is dedicated to sharing his knowledge and democratizing education on various fields such as: Databases, Cloud, AI, and Software Development. His commitment to simplicity and knowledge sharing defines his impactful presence in the tech community.