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:
- Visual Studio 2010 Beta 2’s Modeling Tooling
- Writing code to inject data into the database
- A simple WPF application to inject data into a SQL Server database using the ADO.NET Entity Framework
- We are going to write some code. I love the way this code looks. That is the point of this stuff – to make it easy to write code and interact with data.
The solution built in the previous blog post:
- The Server Explorer is connected to our SQL Server 2008 Instance, showing the bterkaly47.BlogDB.dbo. Notice we have 4 tables shown. PostTag is an artifact of a many-to-many relationship with Post and Tag.
- The Entity Model Diagram is the original model that we built by hand. It later became the physical database that you see in #1 (above this).
- Solution Explorer shows the project that we created. The project type is WPF.
- The Properties window for our entity model.
- The Model Browser allows you to explore your entity model in a hierarchical manner. You can also make modifications.
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.”
- Circle 1 – The command button whose caption we will change
- Circle 2 – The content property for cmdAddData
- Circle 3 – The resulting XAML – You don’t need to mess with it yet
- Circle 4 – Where we name the control “cmdAddData”
After double clicking on the “Add Data” button, you will be brought to the windows below:
- Circle 1 – MainWindow.xaml.cs
- Circle 2 – cmdAddData_Click() event procedure
Notice that on lines 6 & 7 we have defined the attributes of the button through XAML.
Code Listing for MainWindow.xaml
Write some code to inject some data
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.
- Circle 1 – Create a new Blog object
- Circle 2 – Create a new post object
- Circle 3 – Add the post objects to the blog object to fulfill the 1-to-many
Code for MainWindow.xaml.cs
Viewing the Data
Visual Studio 2010 Beta 2 – Strong Modeling Capabilities
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.
Write intuitive Code
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.