This post builds upon our previous blog post about the ADO.NET Entity Framework. The goal is to leverage the Visual Studio 2010 modeling tools to write applications with the ADO.NET Entity Framework.
We will explore:
ADO.NET Entity Framework and .NET 4 – How to use Visual Studio 2010 Modeling Tools to Build a Database
The solution built in the previous blog post:
Navigate to MainWindow.xaml
View the toolbox
Drag a button to MainWindow.xaml
If you double click the button in design mode, the event handler is generated for you. In addition, you are automatically placed in the code with the editor so you can begin programming.
Before we write any code, let’s change the “Content” property of the button control we added. Change the “Content” Property to “Add Data.” You will need to bring up the “Properties Window” by selecting the button (once it’s been dragged to the design surface) then hitting “f4” or “View / Properties Window.”
After double clicking on the “Add Data” button, you will be brought to the windows below:
Notice that on lines 6 & 7 we have defined the attributes of the button through XAML.
Code Listing for MainWindow.xaml
The built in tooling and Intellisense makes code writing a joy. Everything is a selectable popup.
(1) cmdAddData is the event procedure that instantiates the BloggingModelContainer()
(2) & (3) Illustrate the power of intellisense and the entity framework’s built in support coding constructs.
(3) Notice that a “Blog” has many “Post” entries – a 1-to-many. So let’s create one “Blog” object and multiple “Post” objects.
Notice that the “Blog” object has a “Posts” collection. That’s why the code below reads the way it does; we need to ad post objects to the blog object.
Code for MainWindow.xaml.cs
Viewing the Data
Visual Studio 2010 out of the box makes it easier than ever to write entity model based applications. All the built in tooling is there at your finger tips to use.
The code is so easy to read with some of the automatic data typing features. The ADO.NET Entity Model delivers on it’s promise to make code more friendly. The syntax is clean and the relationships in the underlying database are captured by the framework. For example, we can see that a “Blog” object automatically contains an array of “Posts.”
See my friends, Rob Bagby’s post for more information about the loading issues of child collections, such as the “Posts” collection in the “Blog” object.