Sunday, December 28, 2008

Practical Entity Framework - Creating your first data display

This blog post is the first in a series of posts to explore how to use the Entity Framework from a practical standpoint.  The focus will be less on the "about" of Entity Framework and more on the "how".

This series is oriented to those developers who are interested in adopting the Microsoft Entity Framework as their primary data access development methodology.

My examples will use the Microsoft AdventureWorks database in SQL Server 2005 and Visual Studio 2008 SP1.  To provide some variety in the blogosphere, I will use Windows Forms and Visual Basic .Net rather than ASP.Net and C#.

Create a standard Windows Project

Using Visual Studio 2008 SP1 or later, create a new Visual Basic Windows project.


Next add an ADO.NET Entity Data Model template.


Choose the option to generate the model from an existing database.


Choose an existing connection to the AdventureWorks SQL Server database, or create one if needed.


Select the items to be included in the model..


Click Finish.  The template will do its magic and create the entity data model.  You will notice that there is now a Model Browser panel in Visual Studio.


Modify the default windows form to have a DataGridView and a button as shown below.

EF003_Run with Blank Grid and button.

The code behind for the form to display the Product table is simply as follows:

Imports System.Data.Objects
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
Call DisplayDataInGrid()
End Sub

Private Sub DisplayDataInGrid()
' Create the ObjectContext.
Dim context As ObjectContext = _
New ObjectContext("name=AdventureWorksEntities")

Set the DefaultContainerName for the ObjectContext.
context.DefaultContainerName = "AdventureWorksEntities"

Dim query As ObjectQuery(Of Product) = _
New ObjectQuery(Of Product)("Product", context)

Me.DataGridView1.DataSource = query
End Sub
End Class

And the result is:

EF005_Run Image.

Hope that helps.

Joe Kunk

Okemos, MI

My First Expression Blend Image

My daughter was given a math assignment to compute the surface area of a rectangular solid.  Seemed like the perfect excuse to try out the vector drawing capabilities of Microsoft Expression Blend 2.

It took me a little while to get the hang of the environment, but I managed to make what I believe is an acceptable drawing.  I was very pleased with the rich feel of the software and look forward to working in it more.

Abby's Rectangular Prism

Joe Kunk
Okemos, MI


SQL Server DBA Best Practices

A recent issue of SQL Server Magazine had a poster entitled "Brad M. McGehee's SQL Server DBA Best Practices", included courtesy of redgate software. 

The poster is a wealth of great information on how to be successful with Microsoft SQL Server.  Thank you redgate!

In case you missed it, you can get your own free copy of the poster or find a copy of the text on Brad's blog

It is definitely worth your time to give it a careful read.

Hope that helps.

Joe Kunk
Okemos, MI

Saturday, December 20, 2008

Visual Basic Team Releases Visual Basic 2008 Keyboard Shortcuts Poster

The Visual Basic team has released a poster of the Visual Basic 2008 keyboard shortcuts or "Keybinding Reference Poster" as it is listed in the download web page.


It is available here.


Hope that helps.

Joe Kunk
Okemos, MI

Monday, December 01, 2008

Still use LINQ to SQL? Yes!

Tim Mallaileu, Program Manager on the Entity Framework team caused a big stir when he wrote the post Update on LINQ to SQL and LINQ to Entities Roadmap. It was such a big stir that he felt compelled to write a follow up post to clarify it: Clarifying the message on L2S Futures.

The essential message is that the Data Programmability team is de-emphasizing LINQ to SQL in favor of development of the Entity Framework product.  It will still be available in Visual Studio for at least 10 years, but developers should not expect major improvements.

That leaves a conundrum for developers that have invested their precious time in mastering LINQ to SQL and have used it or are considering using it in production applications, including myself.  Should we continue to use LINQ to SQL?  I believe the answer is Yes!.  The simple truth is that the Entity Framework is a v1 product and still has many shortcomings.  I have nothing against the Entity Framework, I'm co-authoring a book on it. 

The productivity boost available today from LINQ to SQL cannot be matched by Entity Framework.  If you are starting development of a long-term strategic line-of-business application, then by all means adopt Entity Framework as your data-access strategy.  For most everyday business applications where the objective is to quickly deliver the desired functionality and then move onto the next burning fire, LINQ to SQL provides a quick and effective data access mechanism that significantly shortens the development cycle.  Use LINQ to SQL and enjoy it.  I'm sure your clients will forgive you for your transgression as they enjoy their new software ahead of schedule and under budget.

In summary, the vast majority of business applications are a great use case for LINQ to SQL and will continue to be until Entity Framework is a rev or two further down the road.


Hope that helps.

Joe Kunk
Okemos, MI