Here are some of the modest goals of this blog post. We will learn:
Let’s continue on from the previous 3 posts about the entity framework:
Previous Blog Entries
Syntactically clean code to view data
I keep addressing the need to have intuitive code. You’ll see shortly that our code is going to look pretty sweet, thanks to the ADO.NET Entity Framework.
We’re going to write code that reads from our context object from the Entity Framework. Our code this time around will loop through parent and child entities. That is the main lesson here.
Getting our WPF Application ready
If you’ve been reading the previous posts, we’ve been building up a WPF application. Here is the source code in case you haven’t seen the other posts. It will be much easier if you just build up the database from the model as my posts describe. You’ll have a much better understanding of how the pieces fit together.
Source Code From Previous Blog Post
The Database Code
Here is the database code for BlogDB. If you followed the previous posts, you won’t need this script. Otherwise, open up SQL Server Management Studio 2008 and run this script.
We will need to do the following:
- Bring in a background image for our main window. I want our applications to be clear in purpose.
- Add another button to view data
|Purpose of Screen: This screen is where we left off. The zip file holds it above.
How To Get This Screen: : Built in previous posts
Adding a background image for our main window
First add an “Images” folder
Right mouse click on your project and select “Add / New Folder”
|Purpose of Screen: Adding a new folder
How To Get This Screen: : Right mouse clicking on project folder
We will now add an image to the images folder.
|Purpose of Screen: Illustrates the “Images” folder
How To Get This Screen: : Follow the step above to add the “Images” folder
You’ll need the folder path to your image to complete the next step:
Add an “Existing Item.”
|Purpose of Screen: To add an image to the images folder. This image will serve as the background to a WPF Window.
How To Get This Screen: : Right mouse click on the “Images” folder
Select the image. You can download my copy here.
|Purpose of Screen: To choose an image to add to the “Images” folder
How To Get This Screen: : You need to “Add / Existing Item” as explained above
Finally, we have the “Images” folder ready with the image.
|Purpose of Screen: The completed solution explorer
How To Get This Screen: : From the “View” menu choose “Solution Explorer”
Adding a “View Data” button
I will make the project up this point available here. Teaching the basic skill of dragging a button to the form is not in scope for this tutorial.
Iterating through related collections
- Two nested loops – because we have many “posts” per “blog”
- There is a one to many relationship. I will verify the database schema with you in a moment.
|Purpose of Screen: To show beautiful code. No ugly queries. Just a nice intuitive one to many.
How To Get This Screen: : You have to type in the code above (or download the final zip)
The running application using ADO.NET Entity Framework code
Our tree control now has data (populated from the the code above)
- The treeview control gets populated after clicking “View Data” in the application
|Purpose of Screen: Showing the code functioning correctly. Note the tree control that has been populated with data from the BlogDB tables.
How To Get This Screen: : You ran the application by hitting “F5” or “Debug / Continue” in Visual Studio
Review – Verifying our one to many by looking at SQL Server
Entity Models are based on relational data (in our case)
By now it should be obvious to anyone that has followed the last several posts that our entity data is always based on relational data. This is a quick detour back to SQL Server Management Studio 2008 that shows how to add database diagrams with a few mouse clicks.
- Our entity model is based on the relational schema
- This is easily verified in SQL Server
|Purpose of Screen: Using SQL Server Management Studio to show us our relational schema through an automatically generated database diagram
How To Get This Screen: : Right mouse click as seen above
Add all of our tables
We would like to see all the relationships so we’ll select all the tables.
- Note that the junction table is needed below for the many to many relationship between Posts and Tags.
- The juncture table is called PostTag
|Purpose of Screen: Adding tables for diagramming purposes (in SQL Server Management Studio)
How To Get This Screen: : Follow previous steps
You can see that there are various relationships that our entity modeling tools automatically leverage
Note the following:
- Blogs to Posts is a one to many relationship
- There is a many to many from Posts to Tags, and hence the need for the juncture table (PostTag)
- These complexities were abstracted away from our business code with the ADO.NET Entity Framework
The main lesson is awesome looking code that is easy to read
Hopefully you learned a few more things when working with the ADO.NET Entity Framework:
- How to insert data using the Entity Framework with C# inside a WPF application
- How to read data using the Entity Framework and populate a Tree View control to depict a one to many relationship.