ADO.NET Entity Framework and .NET 4 & Visual Studio 2010 Modeling Tools – Writing Code And Working with Entities to Add Data

Introduction

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.

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:

  1. 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.
  2. 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).
  3. Solution Explorer shows the project that we created. The project type is WPF.
  4. The Properties window for our entity model.
  5. The Model Browser allows you to explore your entity model in a hierarchical manner. You can also make modifications.

image

image

image

Navigate to MainWindow.xaml

image

View the toolbox

image

Drag a button to MainWindow.xaml

Untitled-1

image

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”

image

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

image 

Notice that on lines 6 & 7 we have defined the attributes of the button through XAML.

Code Listing for MainWindow.xaml

Code Snippet

  1. <Window x:Class="DemoFeatures.MainWindow"
  2.         xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
  4.         Title="MainWindow" Height="350" Width="525">
  5.     <Grid>
  6.         <Button Content="Add Data" Height="38" HorizontalAlignment="Left"
  7.          Margin="69,54,0,0" Name="cmdAddData" VerticalAlignment="Top" Width="113" Click="cmdAddData_Click" />
  8.     </Grid>
  9. </Window>

image

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.

image

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

image

Code for MainWindow.xaml.cs

Code Snippet

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14.  
  15. namespace DemoFeatures
  16. {
  17.     /// <summary>
  18.     /// Interaction logic for MainWindow.xaml
  19.     /// </summary>
  20.     public partial class MainWindow : Window
  21.     {
  22.         public MainWindow()
  23.         {
  24.             InitializeComponent();
  25.         }
  26.  
  27.         private void cmdAddData_Click(object sender, RoutedEventArgs e)
  28.         {
  29.             using(var context = new BloggingModelContainer())
  30.             {
  31.                 var blog = new Blog()
  32.                 {
  33.                     Id = 1,
  34.                     Name = "Blog about developing software",
  35.                     Owner = "Bruno Terkaly"
  36.                 };
  37.                 var post1 = new Post()
  38.                 {
  39.                     Id = 1,
  40.                     BlogId = 1,
  41.                     CreatedDate = DateTime.Now,
  42.                     ModifiedDate = DateTime.Now,
  43.                     PostContent = "Read this blog post by Bruno",
  44.                     Title = "Learning ADO.NET Entity Framework - Intro"
  45.                 };
  46.  
  47.                 var post2 = new Post()
  48.                 {
  49.                     Id = 2,
  50.                     BlogId = 2,
  51.                     CreatedDate = DateTime.Now,
  52.                     ModifiedDate = DateTime.Now,
  53.                     PostContent = "Read this blog post by Bruno",
  54.                     Title = "Learning ADO.NET Entity Framework - Advanced Topics"
  55.                 };
  56.                 blog.Posts.Add(post1);
  57.                 blog.Posts.Add(post2);
  58.                 context.Blogs.AddObject(blog);
  59.                 context.SaveChanges();
  60.  
  61.  
  62.  
  63.             }
  64.         }
  65.     }
  66. }

Viewing the Data

Untitled-1

Conclusions

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.”

Code Snippet

  1. var blog = new Blog()
  2.                 {
  3.                     Id = 1,
  4.                     Name = "Blog about developing software",
  5.                     Owner = "Bruno Terkaly"
  6.                 };

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.

https://www.robbagby.com/entity-framework/is-lazy-loading-in-ef-4-evil-or-the-second-coming/

Download the code here:

https://brunoblogfiles.com/zips/DemoFeatures2.zip