10/31/2007

Entity Framework Goodness

I finally got around to the Entity Framework BETA 2, and I like it already. To play with this, you need these bits;

Entity Framework Beta 2:
http://www.microsoft.com/downloads/details.aspx?FamilyID=f1adc5d1-a42e-40a6-a68c-a42ee11186f7&displaylang=en

Tools, Aug 2007 CTP:
http://www.microsoft.com/downloads/details.aspx?FamilyId=09A36081-5ED1-4648-B995-6239D0B77CB5&displaylang=en

There is also a nice "Getting Started" piece on codeplex:

Getting Started:
http://www.codeplex.com/adonetsamples/Release/ProjectReleases.aspx?ReleaseId=7792

I decided to brave it and just roll a quickie on my own from the Northwind database. All you do is Add New Item / ADO.NET Entity Data Model, which gives you a wizard to choose what database objects you want modeled, and then you get a nice class diagram:

EntityDesignerDiagram

All your entity model code is built for you. So to say, get 10 products from the Products table via a nice little LINQ query:

// Get 10 products via Entity LINQ query
using (NorthwindModel.NorthwindEntities1 ent = new NorthwindEntities1())
{
Products[] prod = (NorthwindModel.Products[])
(from pr in ent.Products select pr).Take(10).ToArray();
GridView1.DataSource = prod;
GridView1.DataBind();
}


LINQ is extremely cool. As with any new technology, there is quite a learning curve, but it's really worth the effort. You can get into all kinds of complex LINQ queries and joins, the vocabulary and the syntax, especially combined with Lambda expressions, is outstanding. I've been working out of the Albaharis' book from O'Reilly, "C# 3.0 in a Nutshell", which has extensive reference-book type material on LINQ. They also have a free downloadable "LINQPAD" executable which i highly recommend.

Oh, and just for the sake of being concise: this is for Visual Studio 2008 - Orcas, NOT Visual Studio 2005.

Sweet!