Sunday, August 16, 2009

LINQ and Entity Framework Posts for 8/10/2009+

Note: This post is updated weekly or more frequently, depending on the availability of new articles.

Updated 8/15/2009: Added Jesse Liberty’s LinqToSQL: A Detailed Review
• Updated 8/15/2009: Additions

Entity Framework and Entity Data Model (EF/EDM)

Beth Massi demonstrates Auto Access to non-Indexed Collections : Or How I Learned to Stop Worrying and Love the VB Compiler in this 8/14/2009 post. She notes that C# throws and error and VB doesn’t with the following example:

I have an EDM defined with a parent entity Order and child OrderDetails that I created with the designer – very basic – like I show in this video. In the example I was working on, I was putting the results of an order query into a generic list but also including the OrderDetails so I end up with an EntityCollection called OrderDetails on every Order.

OMSEntities db = new OMSEntities();

var results = from o in db.Orders.Include("OrderDetails")
              select o;

List<Order> orderList = new List<Order>(results);

However if I want to get the first Order’s first OrderDetail, I get an error in C#: “Cannot apply indexing with [] to an expression of type 'System.Data.Objects.DataClasses.EntityCollection<EFTestDAL.OrderDetail>'” when I write this line:

//ERROR
Console.WriteLine(orderList[0].OrderDetails[0].OrderDetailID);

However, the VB code works perfectly.

Dim db As New OMSEntities

Dim results = From o In db.Orders.Include("OrderDetails") _
              Select o

Dim orderList As New List(Of Order)(results)

'No Error
Console.WriteLine(orderList(0).OrderDetails(0).OrderDetailID)

Alex JamesTip 32 – How to create a database from SSDL – EF 4 only post of 2/11/2009 explains that the EF4 (a.k.a. EF v2) Code-Only assembly includes CreateDatabase(), DropDatabase(), DatabaseExists() and CreateDatabaseScripts() extension methods that you can use “with any ObjectContext, regardless of whether Code-Only was used to create it.”

The Entity Framework Design Team continues the attempts to kill LINQ to SQL with a new LINQ to SQL to Entity Framework conversion template of 8/13/2009.

This template converts LINQ to SQL metadata (.dbml) to metadata compatible with Entity Framework (.edmx). While not a complete solution to the conversion problem, it is a useful first step. The template available for download is still in the development stage, and any feedback you have on the user experience, additional features, or any other aspect of the conversion would be greatly appreciated.

The T4 template is intended to convert simple valid .dbml files to .edmx files. All of the basic elements of the .dbml file are mapped to their .edmx counterparts. For instance, Tables are mapped to EntitySets and Types are mapped to EntityTypes. Additionally, stored procedures (including customized insert, update and delete stored procedures) and associations are also converted.

As many have noted, LINQ to SQL just won’t die.

Julie Lerman explains EF Table Splitting – The Opposite of Entity Splitting with EF4 (EFv2) beta 1 in this 8/11/2009 post.

Alex JamesTip 31 – How to compose L2O and L2E queries explains how to partition EF queries into LINQ to Entity and LINQ to Object components in this 8/10/2009 post.

Julie Lerman asks EF4 – New Properties for Entity Properties? of 8/10/2009 and answers:

If you are working with the Beta 1 of VS2010 in an Entity Data Model, you might notice some new properties in the Properties Window for an entity’s scalar property.

propertyfacetsA

Are they new properties? Actually only one of them is.

LINQ to SQL

•• Jesse Liberty’s LinqToSQL A Detailed Review of 8/16/2009 is a Turing Project mini-tutorial about LINQ to SQL:

In a previous post I discussed the advantage of separating the Data Access Layer (retrieving data from your data store) from the Data Transport Layer (moving data from the server to your client.  We then took a small but important detour into an introduction to Linq.  This posting will expand upon Linq sufficient to allow a good understanding of how we’ll be using Linq in the rest of the project.

There are three strong candidate technologies for the Data Access Layer (DAL):

  1. ADO.NET
  2. Linq to SQL
  3. Entity Framework

In the next posting we’ll move forward with Entity Framework as it provides an excellent high abstraction of your data that is object-oriented and that works very well with our Data Transport technology of choice: RIA Services.

Damien Guard put together a LINQ to SQL cheat sheet on 8/13/2009 and lets you download it: LINQ to SQL cheat sheet (PDF) (76 KB)

Jeffrey Schwartz’s LINQ to SQL Lives On post of 8/10/2009 to Visual Studio Magazine’s Data Driver Blog observes:

Many database developers have loudly bemoaned Microsoft's decision late last year to marginalize LINQ to SQL in favor of its ADO.NET Entity Framework.

The angst played out as many who build applications designed to access Microsoft's SQL Server felt left holding the bag as reported here . Make no mistake: the Entity Framework is Microsoft's object relational mapping (ORM) technology of choice and that will become even more evident next year with the release of Entity Framework 4, Visual Studio 2010 and the .NET Framework 4.

LINQ to Objects, LINQ to XML

Deborah Kurata’s LINQ’ing to Word and Excel – Deborah shares the magic of Cast (in C# & VB) post of 8/14/2009 explains:

Some of the collections in the Microsoft Office object models implement IEnumerable. The IEnumerable interface provides the ability to perform a for/each against the collection. With .NET 3.5, a Cast extension method of IEnumerable allows you to work with these collections using Linq.

Eric White offers XML Element and Attribute Name Guidelines based on Guidelines for Names from the .NET Framework Design Guidelines in this 8/12/2009 post. Topics include:

  • Pascal Casing For Tags Longer than Three Characters
  • Capitalization Rules for Acronyms
  • Don’t Use Abbreviations
  • Word Selection
  • XML Namespaces

LinqMaster says in his Thread Safe Generic Queue Class post of 8/11/2009:

I've been doing a lot of mult[i]-threading work, recently, using the standard Thead class, the Worker Queue, and the new PLINQ (Parallel LINQ). The problem with most of the built-in generic collections (Queue<>, List<>, Dictionary<>, etc), is that they are not thread safe.

I created a library of thread safe collections which allow me to use the standard generic collection actions (foreach, LINQ, etc), while at the same time being thread safe.

The classes in this library inherit from the appropriate collection interface (IEnumerable, ICollection, etc). Each class also has all the functions and properties that it's original non-thread safe class has.

You can download a copy of the entire library, which includes support for a thread safe List<>, Dictionary<>, and Queue<>, here: Thread Safe Generic Collections

Bart De Smet explains in detail how to experience Statement Trees With Less Pain – Follow-Up on System.Linq.Expressions v4.0 in his post of 8/11/2009, which continues from his Expression Trees, Take Two – Introducing System.Linq.Expressions v4.0 of 8/10/2009.

Alex JamesTip 31 – How to compose L2O and L2E queries explains how to partition EF queries into LINQ to Entity and LINQ to Object components.

ADO.NET Data Services (Astoria)

Bruno Tekaly’s ADO.NET Data Services post of 8/9/2009 provides an overview of the Astoria project with sample URLs for entity sets, entities, and associated entities.

ASP.NET Dynamic Data (DD)

David Hayden’s ASP.NET MVC 2.0 and ASP.NET DynamicData Buddy Classes for Holding Metadata – MetadataType post of 8/10/2009 explains:

In my last post, I talked about how ASP.NET MVC 2.0 Preview 1 has built-in support for validating classes using System.ComponentModel.DataAnnotations:

Absolutely love that as now we don't have to create Custom ModelBinders just to sneak in a more robust validation framework in replace of IDataErrorInfo support in ASP.NET MVC 1.0!

Miscellaneous (WPF, WCF, MVC, Silverlight, etc.)

• Scott Guthrie’s ASP.NET MVC V2 Preview 1 Released post of 7/31/2009 bears repeating for those who might have missed it. Preview 2 will be included in the next .NET Framework 4 drop.

blog comments powered by Disqus