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, 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.
Lifetime Access / Downloadable Resources and 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:
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!
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.
(Lifetime Access/ Live Demos / Downloadable Resources and more!)
Artemakis Artemiou is a Senior Database and Software Architect, Certified Database, Cloud and AI professional, Author, a 9 Times Microsoft Data Platform MVP (2009-2018). He has over 20 years of experience in the IT industry in various roles. Artemakis is the founder of SQLNetHub. Artemakis is the creator of the well-known software tools Snippets Generator and DBA Security Advisor. Also, he is the author of many eBooks on SQL Server. Moreover, Artemakis teaches on Udemy, you can check his courses here.
Views:2,153
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent. Read More
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie
Duration
Description
cookielawinfo-checkbox-analytics
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional
11 months
The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy
11 months
The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.