Thursday, December 20, 2007

LINQ and Entity Framework Posts for 12/20/2007+

Note: There are two linkblog posts this week because of the high frequency of relevant posts/stories.

Dare Obasanjo Analyzes Amazon's SimpleDB

Dare Obasanjo weighs in with his Amazon SimpleDB: The Good, the Bad and the Ugly post of December 21, 2007. He likes "Comoditizing hosted services and getting people to think outside the relational database box," dislikes "Eventual Consistency and Data Values are Weakly Typed," and detests "Web Interfaces, that Claim to be RESTful but Aren’t." I agree with Dare on all topics except eventual consistency. GET requests for data updates are atrocious.

My original Amazon Announces Beta of SimpleDB Web Services in the Cloud post has had gone through several updates since it's December 14, 2007 posting.

Matt Sollers Overcomes LINQ to SQL's Challenges and Limitations

Matt's December 21, 2007 Compact_LINQ contribution to CodeProject covers the following topics, some of which have received scant coverage by others:

His sample time-tracking project, which uses SQL Server Compact Edition (SSCE) as the LINQ to SQL data source, is available for download.

Added: 12/23/2007

Two Out of Three Connect Bug Reports for VS 2008 Unacknowledged After Two Months

A link to a proposed redesign of the Microsoft Connect bug-tracking service reminded me that the VS 2008 team hadn't replied to two of my three latest bug reports in about two months.

Prompt acknowledgment of and eventual reply to bug reports is far more important than adding color and illustrations to the pages.

Added: 12/23/2007

Danny Simmons Answers My EntityCollection Depth and Serialized EntityReferences Issues

Danny's Filtered association loading and re-creating an entity graph across a web service boundary... post of December 21, 2007 answers the issues I raised in my Controlling the Depth and Order of EntitySets for 1:Many Associations post of December 20, 2007:

He explains that you can control the depth of associated EntitySets by using the EntityCollection.Attach() method with a predicate, as in:

customer.Orders.Attach(customer.Orders.CreateSourceQuery().Where(order => order.Date >= new DateTime(2007, 1, 1)));

as well as with three other approaches. The EntityCollection.CreateSourceQuery Method help topic says:

Returns an object query that when executed returns the same set of objects that exist in the current collection.

but doesn't provide sample code. A search on 'CreateSourceQuery' returns valid hits to online help and Danny's post only, so there doesn't appear to be any sample code for the method, which also has an EntityReference counterpart.

It appears to be possible to take advantage of the ObjectStateManager's stubs to regenerate EntityReferences on the client side of a Web service in a three-step process:

  1. Serialize and transport the target and reference entities with their key to the client
  2. Deserialize and attach the target entities to the target entities
  3. Deserialize and attach the reference entities to the target entities' stubs

Danny's general-purpose container sample will use the above approach.

Thanks for taking the time for such a detailed reply, Danny. I'm in the process of updating my post now.

Added: 12/23/2007

Scott Hunter Catalogs ASP.NET Dynamic Data Features

In his Breakdown of Dynamic Data Features of December 21, 2007, Scott provides a detailed, outline-style listing of the features that ASP.NET delivers. He emphasizes that most Dynamic Data features aren't scaffolding-dependent.

Added: 12/23/2007

Mike Taulty Explores Filtering Across Astoria Associations

His ADO.NET Data Services - Filtering Across Assocations post of 21 December 2007 shows the syntax for filtering many:1 associations between Northwind Customer and Order entities:


With an ASP.NET service and default de-pluralized LINQ to SQL entity names, my corresponding query looks like this:


Mike asks "How to navigate the other way foxed me a lot more (is this stuff written down somewhere? :-))."

Here are 1:many association examples from the ADO.NET Data Services' Simple Addressing Scheme for Data with Uniform URLs QuickStarts page:

http://myserver/data.svc/Customers('ALFKI')/Orders?$filter=Active eq true

http://myserver/data.svc/Customers('ALFKI')/Orders?$filter=Active eq true and (year(OrderDate) eq 2007)

Obviously, the preceding queries weren't tested because Northwind.Orders doesn't have an Active field and the year() function throws the following error with LINQ to SQL or EF as the service's data source:

No applicable function found for 'year' at position 1 with the specified arguments. The functions considered are: year(System.DateTime).

However, the following URI filters orders from ALFKI with Freight >  $50:


The following returns all Products and their Category for each Supplier:


(Pablo Castro says there's no support for $expand by the LINQ API.)

So far, I haven't been able to make LINQ to REST joins work with either EF or LINQ to SQL service data source. This LINQ expression

var query = from c in context.CreateQuery<Customer>("Customers")
            join o in context.CreateQuery<Order>("Orders")
            on c.CustomerID equals o.CustomerID
            where c.Country == "USA" && o.Freight > 50
            orderby c.CompanyName
            select c;

compiles without complaint, but creates the following (suspicious) query string:


that throws this runtime error:

Can only specify query options (OrderBy, Where, Take, Skip) after last projection.

I'm working in the blind on more complex LINQ to REST queries.

Added: 12/23/2007

Frans Bouma Tackles OrderBy, Aggregates, and GroupBy for LINQ to LLBLGen Pro

In his December 21, 2007 Developing Linq to LLBLGen Pro, part 10 post, Frans continues the description of his quest to LINQ-enable LLBLGen Pro with more Standard Query Operators (SQOs)—OrderBy(), ThenBy()OrderByDescending(), ThenByDescending(), Sum(), Avg(), Min(), Max(), Count(), LongCount(), and GroupBy().

It took three weeks for Frans to get GroupBy() to work with aggregates. Read his article to see why.

Added: 12/21/2007

Marcin Dobosz Describes ASP.NET Dynamic Data Foreign Key Bug and Fix

Marcin's Dynamic Data December preview context caching bug fix post of December 20, 2007 describes a problem relating to updating drop-down lists of foreign key filters and relation columns, and points to a fix for the problem.

Added: 12/21/2007

Guy Burstein and ADO.NET Data Services Update Issue with LINQ to SQL

Guy's ADO.Net Data Services Feedback: DataContextServiceProvider Required for LINQ to SQL support post of December 20, 2007 confirms Mike Taulty's discovery that LINQ to SQL's DataContext must support IUpdatable() in order to update an Astoria data source (see Below). Guy concludes:

Using ADO.Net Entity Framework as the service data context gives you update support for free. If you are using LINQ to SQL, you can implement IUpdatable yourself, as Mike has done here. I think that the product team should consider adding a DataContextServiceProvider class that uses the LINQ to SQL DataContext capabilities for updating the store.

I agree.

Added: 12/21/2007

Controlling the Depth and Order of EntitySets for 1:Many Associations

LINQ to SQL offers the DataLoadOptions.AssociateWith() method, which accepts a lambda expression as its argument that lets you apply LINQ Standard Query Operators to filter and order the members of EntitySets eager-loaded for association properties. If the Entity Framework (EF) has a corresponding feature, I haven't been able to find it.

Another Entity Framework issue that bothers me is the loss of EntityRefs when serializing entities with WCF's DataContractSerializer but the NetDataContractSerializer that can handle cyclic relationships (cycles) isn't interoperable with most other languages and platforms. LINQ to SQL has the same problem but, in my opinion EF requires fully serializable object graphs to play in the enterprise object/relational mapping (O/RM) tool market.

Read Controlling the Depth and Order of EntitySets for 1:Many Associations for more details and enlighten me on how to control EF EntitySet depth and order.

Update 12/23/2007: These issues have been resolved, at least in part. See "Danny Simmons Answers My EntityCollection Depth and Serialized EntityReferences Issues" above.

"Visual Studio 2008 Kicks Off" Quartet in Visual Studio Magazine's January 2008 Issue

Vol. 1 "Visual Studio 2008 Kicks Off"

The cover story for VSM's January 2008 issue is the first of my four articles about VS 2008's debut. Here's the deck:

Visual Studio 2008 is all about integrating new .NET Framework 3.x Web and smart-client technology, increasing developer productivity, and managing the application lifecycle. Find out how VS 2008's greatly expanded feature set and new .NET Fx 3.5 namespaces can boost your career as a professional developer and add to your programming enjoyment.

Vol. 2 "VS 2008 Kicks Off: The MIA Features"

Visual Studio 2008 is an ambitious new release and it includes a slew of new language features and tools that were required to get LINQ up and running by itself and with SQL Server 200x. Of course, not everything planned made it into the product. Here's a description of the elements that were pared back and what their status is now.

Vol 3 "VS 2008 Kicks Off: The Road Ahead"

Microsoft was already well on its way to creating the next version of Visual Studio (code-named "Hawaii") when it released VS 2008. Learn what the future holds for .NET developers.

Vol 4 "VS 2008 Kicks Off: It Comes in Many Shapes and Sizes"

Visual Studio 2008 stock-keeping unit (SKU) names for the Express, Standard, and Professional editions remain the same as for Visual Studio 2005 except for the year. Visual Studio 2005 Team Edition SKUs have been renamed to Visual Studio Team System 2008 Edition Name.

Calling the last member of the quartet an article or volume instead of a sidebar is a stretch but I've always been envious of Lawrence Durrell.

Bill McCarthy and Kathleen Dollard round out the VS 2008 Kicks Off topic with A Guide To VB 2008 and Exploring C# 3.0 articles.

Danny Simmons: An Essay on Data-Centric Web Services with Entity Framework

Part 1, Why are data-centric web services so hard anyway? of December 19, 2007, "begin[s] a discussion about creating web services which are built on top of the Entity Framework and enable the exchange of entity data," analyzes competing requirements (such as the flexibility of service operations and interoperability) and describes tactical problems (like graph serialization and change tracking.)

At present, graph serialization seems to me to be more a strategic morass than a tactical problem.

Part 2, So they're hard, but what if I need them[?] ... of the same date, discusses the challenges of designing and implementing data-centric Web services with the Entity Framework:

  1. The traditional SOA / DTO approach.
  2. Constrain your scenarios and use standardized operations. 
  3. With graph-serialization you could roll-your own SOA system without separate DTOs.
  4. General-purpose container object.
  5. Automatically generate DTOs from a declarative description of the contract.

I'm eagerly awaiting a concrete implementation of the abstract concept of challenge #4. #5 seems to me to be a long way out.

If you're interested in the issues that confront Web service architects dealing with O/RM tools as the data access layer, don't miss these two posts!

Diego Vega Lists Differences Between the Capabilities of eSQL and LINQ to Entities

In Some differences between ESQL and LINQ to Entities capabilities of December 20, 2007, Diego separates differences into these categories:

  • What is almost the same
  • What is somewhat different
  • What is way different

John Papa's related Choosing an API to Talk to the Entity Framework post of the same date has recommendations for choosing from these options:

  1. Entity SQL with Entity Client
  2. Entity SQL with Object Services
  3. LINQ with Object Services

Zlatko Michailov Continues His Treatise on the Edm.Length() Canonical Function

Zlatko's The Edm.Length() Canonical Function over SqlClient of December 19, 2007 concludes that the SQL Server team needs to add a new string length function to eliminate the conflicts between T-SQL's and EF's definition of string length for varchar strings with trailing spaces.

Mike Taulty Describes Problems with Disabling LINQ to SQL's Optimistic Concurrency

Sometime you might want to enable "last entry wins" (i.e., no concurrency management) for a LINQ to SQL projects. Mike's LINQ to SQL - Tailoring the Mapping at Runtime post of December 20, 2007 shows just how difficult that can be.

Erick Thompson Contrasts Two Packaging Namespaces

In What is the Difference Between the System.IO.Packaging and Microsoft.Office.DocumentFormat.OpenXml.Packaging Namespaces? of December 20, 2007, Erick explains:

There are two separate namespaces, each containing a number of classes, which you can use to open and get at the contents of Open XML documents.

Erick notes that objects from the OpenXml.Packaging namespace are strongly typed and require less code than those of System.IO.Packaging.

Guy Burstein Continues His ADO.NET Data Services Series

ADO.Net Data Services Part 3 - Querying a Web Data Service and WebDataGen.exe of December 19, 2007 shows you how to build a WCF client to query the Web Services built in ADO.Net Data Services Part 2 - Using Service Operations with WebGet.