Monday, May 18, 2009
The Entity Framework - Part 2 - Inheritance
This post discusses about one of those features; Inheritance.
Inheritance allows creating entities having as base type other entities, thus allowing constructing even more sophisticated data models with ease.
The introductory post provided information on how you can create an entity data model by connecting to a SQL Server 2008 database.
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 columnn 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.
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.
Until next time!
Easily secure your SQL Server instances! Try out DBA Security Advisor!
DBA Security Advisor was developed by SQLArtBits. The tool was being carefully developed for an entire year and it contains comprehensive security checks and recommendations based on proven SQL Server security best practices. Feel free to download and use the Community Edition which is free and if you find it useful, you can consider upgrading to the Enterprise Edition!
Artemakis Artemiou [MVP]
Author & Editor
Artemakis Artemiou is a Senior SQL Server Architect, Software Developer and Microsoft Data Platform MVP. He is also an author, regular blogger, president of Cyprus.NET User Group and CY Country leader of INETA-EU. He is also the creator of DBA Security Advisor and In-Memory OLTP Simulator. Artemakis is a frequent guest author of worldwide well-respected online journals where he writes articles focusing on many SQL Server topics.
Reference: The SQL Server and .NET Hub (http://www.sqlnethub.com)