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 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:
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:
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:
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):
I hope you found this post useful. The Next post in the Entity Framework series will be dedicated to Entity Splitting.
Until next time!