Archive for March, 2009

Entity Framework – Yet another Step by Step Guide

Monday, March 30th, 2009

Here at Coalition Software we’ve been toying with the idea of using the Microsoft Entity Framework for some time now. It wasn’t until we saw the great demonstration by Nick Goossens at DevDays 2009, that we’ve decided to start using it for our projects.

So I’ve decided to do another Step-by-Step guide, to document our journey and learning experience in the Entity Framework world.

Getting Started

The Database

First we need a database, head over to Codeplex, and download the Adventure Works LT database. For this guide, we’ll look at the Customer, SalesOrderHeader, SalesOrderDetail and Product tables.

The Code

Next, create a new Visual Studio Windows Application project called AdventureWorksUI next, add a new project to your solution called AdventureWorksEF.

We now have 2 projects within our solution, AdventureWorksUI is where we’ll create our user interface and AdventureWorksEF, will be where all our Entity Framework objects are stored.

In the AdventureWorksEF project add a new ADO.NET Entity Data Model, called AdventureWorksModel.edmx. Click on Generate from database, create a new connection to the database. Then select the Customer, SalesOrderHeader, SalesOrderDetail and Product tables, as shown below(click for a larger image).

image

Leave the Model Namespace as AdventureWorksLTModel. Click Finish.

The UI

Next, add a reference to your AdventureWorksEF project from the AdventureWorksUI project. Open Form1.vb in design view, and place a datagrid called DataGridView1 and a button named button1 on the form. Switch to code view and the button’s Click event. Enter the following code, and hit F5.

        Dim ctx As new AdventureWorksEF.AdventureWorksLTEntities
        Dim result = From c In ctx.Customer _
                     Where c.Title = "Mr." _
                     Order By c.LastName
        DataGridView1.DataSource = result

NOTE: You will receive the following error:

The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.

The fastest workaround for this is to copy the App.Config file from your AdventureWorksEF project to your AdventureWorksUI project.

Also If the compiler complains add a reference to System.Data.Entity

If everything goes well, you should see a list of all customer with a title of Mr. If the above code looks completely weird for you, go do some reading on LINQ, you can find some great tutorial videos here.

Saving data

Add another button to the form, and enter the following code to it’s Click event.

ctx.SaveChanges()

You would need to move the

Dim ctx As New AdventureWorksEF.AdventureWorksLTEntities

line to the top of your class in order for this to work.

Now, make some changes in the grid, hit the save button and the changes will be saved to your database! Easy.

In the next few instalments of this guide, we’ll look at data binding and loading child objects. Until next time, keep coding!

New Year, New Look

Monday, March 30th, 2009

Well, It’s been some time since I’ve last blogged and as you can see Mythical Man Moth has a new look. Unfortunately due to SubText’s export functionality not working, some comments went missing.You’ll notice that you can now chat to me directly via Windows Live Messenger by clicking on this icon .

I’ve switched from SubText to BlogEngine.Net, reason being, BlogEngine.Net has a lot more cool features as I’ve discovered whilst setting up a blog for JC Oberholzer, go check out his musings at www.jcoberholzer.com

This year did start off with a bang, we’re pretty busy at Coalition Software with a brand new Top Secret project…of which all will be revealed at a later stage on this blog.

I’ve attended DevDays 2009 last week, and we were so impressed with Microsoft’s Entity Framework that we will start using it…not that we do not love CSLA anymore, but for a lot of our projects we do not need a lot of the functionality that CSLA provides.

So, even though it might be a bit late, welcome to Mythical Man Moth for 2009! Keep watching this space.


Google Analytics integration offered by Wordpress Google Analytics Plugin