Saturday, January 26, 2008

LINQ and Entity Framework Posts for 1/21/2008+

Mike Tulty Produces Three More ADO.NET Data Services Video Clips

His ADO.NET Data Services - Screecasts of January 25, 2008 has links to

which supplement these

produced in mid-January.

Added: January 25, 2008

Julie Lerman Implements EntityBag in a Visual Basic Service and Client

Julie's Creating and consuming a WCF service to test the EntityBag class post of January 25, 2007 includes details for employing Danny Simmons' EntityBag code in a VB WCF service and client. She warns that it's necessary to decorate your interface's ServiceOperation functions with <ServiceKnownType(GetType(EntityType))> for serialization to work correctly.

By default WCF implements WS-* security with wsHttpBinding, which greatly increases SOAP message overhead, as Julie notes in her Testing out the EntityBag post of January 24, 2008. You can use the WCF Service Configuration Editor to change the service to basicHttpBinding, which defaults to clear text messages.

Suggestion: I believe it would behoove the ADO.NET Team to provide a simple reference project with a self-hosted service based on Northwind or AdventureWorksLT using the basicHttpBinding. This would save everyone that downloads the Perseus classes a substantial amount of time and guesswork on implementation details.

Added: January 25, 2008

Diego Vega: ADO.NET Team Starts Entity Framework Portal on the MSDN Code Gallery Site

Diego's Welcome to the Entity Framework Toolkits & Extensions post of January 25, 2007 announces the creation of the ADO.NET Entity Framework & LINQ to Relational Data Portal on the MSDN Code Gallery. The gallery is a CodePlex implementation that's customized for contributions from Microsoft teams. Content is distributed under the Microsoft Public License (Ms-PL) unless otherwise noted.

The first toolkit and extension are:

  • eSqlBlast is Zlatko Michailov's interactive Entity SQL tool and supporting libraries.
  • Perseus is Danny Simmons' set of EntityBag and related classes for WCF services (see EntityBag topics below).

The first sample is:

Update 1 January 26, 2008: Sanjay's SampleEdmxCodeGenerator sources post provides instructions for using the SampleEdmxCodeGenerator.

Questions: Will EFx become the official abbreviation for Entity Framework and LINQ to Entities be renamed LINQ to Relational Data? I doubt it.

Update 2 January 26, 2008: Diego says in two comments that Asad Khan didn't write the EDMX code generator (but didn't say who wrote it) and that LINQ to Relational Data is a generic term for LINQ implementations for relational data (i.e., LINQ to SQL and LINQ to Entities at the moment).

Added: January 25, 2008

Sanjay Nagamangalam Describes How the ADO.NET Entity Designer Handles CodeGen

Sanjay's How does the ADO.NET Entity Designer generate code? of January 24, 2008 describes how the EntityModelCodeGenerator, which uses VS 2008's SingleFileGenerator extension mechanism, extracts the CSDL content from the EDMX file and generates code with the EntityClassGenerator.

His How to Extract CSDL from EDMX post of the same date shows you how to extract CSDL, MSL, or SSDL content from the EDMX file with LINQ to XML.

Update January 26, 2008: Sanjay's SampleEdmxCodeGenerator sources post provides instructions for using the SampleEdmxCodeGenerator.

Added: January 25, 2008

Danny Simmons Wraps Up His EntityBag Sextet with the RelationshipEntry Story

EntityBag Part VI – RelationshipEntry of January 24, 2008 is the final installment of Danny's six-part EntyBag Extravaganza. RelationshipEntry is a DataContract-serializable class with eight DataMember properties:

  • RelationshipName
  • State
  • Role1
  • Key1
  • AddedEntityIndex1
  • AddedEntityIndex2

and two non-serializable properties

  • Entity1
  • Entity2

Constructor, ResolveEntityAndKeys, and Methods topics explain how the class is used.

The three class files (EntityBag.cs, ContextSnapshot.cs, and RelationshipEntry.cs) and required extension methods (UtilityExtensionMethods.cs) are in the Perseus  Entity Framework Extension file that's described in the preceding "Diego Vega: ADO.NET Team Starts Entity Framework Portal on the MSDN Code Gallery Site" topic.

Added: January 25, 2008

Mike Taulty Minimizes LINQ to XML Code by Using the ReplaceWith() Method

Mike moves two nodes within a child node to its parent with three lines of LINQ to XML code in his Reparenting nodes in LINQ to XML of January 24, 2008.

The more I work with LINQ to XML, the better I like it (despite some reservations about namespace issues I discussed in Namespace Strangenesses in XML Infosets Transformed by LINQ to XML.)

The Pfx Team: Parallelizing LINQ Aggregate Queries with PLINQ

The Parallel Programming with .NET blog's Parallel Aggregations in PLINQ post of January 24, 2008 begins by describing how the Aggregate Standard Query Operator works, then discusses the general topic of parallel LINQ aggregation semantics, and identity issues that can cause the results of parallel and sequential aggregation to differ.

More EntityBag Details Exposed: The ContextSnapshot Object

Danny Simmons' EntityBag Part IV – ContextSnapshot Fields and Properties post of January 22, 2008 and EntityBag Part V – ContexSnapshot Constructing and Applying of January 23 cover the ContextSnapshot that's serialized across the wire. Topics include:

  • The DataMembers and Other Fields: unchangedEntities, modifiedOriginalEntities, modifiedEntities, deletedEntities, addedEntities, addedRelationships, deletedRelationships and unchangedManyToManyRelationships.
  • Public Properties and Related Methods: ConnectionString, Dictionary<EntityKey, int> AddedEntityKeyToIndex and IEntityWithKey GetAddedEntity(int addedEntityIndex)
  • Constructors: ContextSnapshot(ObjectContext context) for the service and ContextSnapshot(ObjectContext context, string connectionString) for the client with a cleared connection string
  • ApplyToContext(ObjectContext context) method: Attaches unchangedEntities, modifiedOriginalEntities and modifiedEntities, adds addedEntities and addedRelationships, deletes deletedEntities and deletedRelationships, and finally attaches unchangedManyToManyRelationships

The ContextSnapshot corresponds to the ephemeral "mini connectionless DataContext" for LINQ to SQL's erstwhile "n-tier story."

Danny's three posts that precede this are:

Following are links to a few of Danny's other related posts (in reverse chronological order):

Added: 1/23/2008

Namespace Strangenesses in XML Infosets Transformed by LINQ to XML

I was working with an Atom 1.0 document returned from an ADO.NET Data Service based on Northwind (what else?) when I ran across two anomalies in how LINQ to XML handles namespaces when composing XML Infosets with VB's literal XML or C#'s functional construction syntax.

VB MVP Bill McCarthy led me to an (apparently undocumented) workaround for a spurious xmlns="" namespace declaration, but I still haven't found the answer to preventing namespace declaration duplication with literal XML composition.

Read more about the issues here.

Added: 1/23/2008

Julie Lerman: Those Who Forget the LinqDataSource Are Bound To Repeat Its Problems in the EntityDataSource

Julie Lerman recounts the problems that she's encountered with the LinqDataSource for LINQ to SQL, especially with lack of updatability when you specify a projection and return an anonymous type, and hopes they won't be repeated in the forthcoming EntityDataSource.

She also points out an anomalous exception when editing with Entity.Property/EntityReference pairs in combo boxes with StoreOriginalValuesInViewState set to True.

I'm not sanguine that the ADO.NET team can overcome these EntityDataSource hurdles by RTM.

Julie observes that the EntityDataSource "has been mentioned in the forums." Danny Simmons said:

While the LINQDataSource will not work with LINQ to Entities, we are working on an Entity Data Source which we expect to ship in v1 of the EF.

in answer to Blinky Bill's Informal List of Limitations entry of 1/3/2008 that spelled out some of the more serious issues with Entity Framework Beta 3. His complaints—together with Danny's replies—are well worth reading.

Here's Danny's same-day response regarding the ObjectContext's lack of serializability:

I would argue that making the ObjectStateManager serializable introduces significant issues long-term for n-tiered stacks.  If it were serializable certain things would be easier, but it's somewhat like shooting yourself in the foot if you are building any kind of large-scale SOA system.  You might take a look at my blog post: http://blogs.msdn.com/dsimmons/archive/2007/12/20/why-are-data-centric-web-services-so-hard-anyway.aspx That said, making n-tiered stacks easier to create is certainly an important topic we are giving a lot of thought to.

It's an interesting response in the light of Danny's current work on the EntityBag, no?

(My apologies to philosopher George Santayana,whose "Those who cannot remember the past are condemned to repeat it" warning comes from Reason in Common Sense, the first volume of his The Life of Reason quintet.)

Alexander Nowak Solves Missing Entity Framework Files Problem with Unit Testing

Belgian developer Alexander Nowak's Unit testing ADO Entity Framework post of January 22, 2008 describes how he encountered "The specified metadata path is not valid" exceptions when attempting to unit test a sample Entity Framework Beta 3 project. The problem is related to issues with the design-time .edmx file and missing .ssdl, .msl, and .csdl from the specified MSTest folder that holds copies of the assembly and app.config or web.config files.

Alexander shows you how use the VS test environment's Deployment feature of the Rosario November 2007 CTP to add the missing metadata files to the appropriate MSTest folder.

Embedding the three metadata files in the assembly by the process Julie Lerman describes in her Embedding EDM schema files in Entity Framework Beta 3 post of December 14, 2007, should also solve the problem. Here's a brief excerpt from her detailed article on the topic.

Open the EDMX file in the design window and then you will see a new property in the property window called Metadata Artifact Processing. The default of this is "Copy to Output".

To get around a current known bug, build the project with the property set to "Copy to Output". Then change the Metadata Artifact Processing property to "Embed in Output Assembly". Eventually you won't need to build with Copy to Output first.

EntityBag Details Exposed: Modes, Constructor, Public Surface and Serialization

Danny Simmons' EntityBag Part II – Modes and Constructor and EntityBag Part III – Public Surface and Serialization posts of January 21, 2008 describe the EntityBag class's enum, objects, factory class and methods:

  1. Mode Enum: Constructed -> Deserialized -> LocalContext -> Unwrapped
  2. Root Object: A single public property over which to create a private ObjectContext
  3. CreateEntityBag<T>(context, root) factory class for data retrieval
  4. OnSerializing(streamingContext) method: if Constructed mode, create ContextSnapshot from persisted entities; if LocalContext mode, create ContextSnapshot from changes to original ContextSnapshot
  5. UnwrapInto<T>(context) method: for create, update, delete operations

In answer to a comment about EntityBag Part I – Goals of January 20, 2008, Danny says:

The EF won't have something like this built-in for v1, but we are looking hard at the topic for future releases.  I'm not 100% certain we'll do this, since there are some serious issues when it comes to interoperability, etc. (as I've noted).  I believe we made some major mistakes with the DataSet in this regard, and I don't want to repeat them.

Marcelo Lopez Ruiz Explains How to Use Service Operations with ADO.NET Data Services

Marcelo's Service Operations in ADO.NET Data Services post of January 21, 2008 explains how to set up ADO.NET Data Services with the RESTful equivalent of Web methods—Windows Communication Foundation (WCF) service operations that expose custom methods decorated with the [WebGet] attribute. The functions return an IQueryable<Model.Entity> or IEnumerable<Model.Entity> type, where Model is the name of the underlying Entity Data Model and Entity is the entity name, such as Customer[s].

Setting up service operations with custom WebGet methods lets you easily restrict particular users or groups to specific retrieval operations against your data services.

The following WebDataService's CustomersInLondon() method returns an IQueryable<Model.Customer> to enumerate all Northwind Customers in London from this URL: http://localhost/WebDataService1.svc/CustomersInLondon.

public class WebDataService1 : WebDataService< Model.Entities >
{
    public static void InitializeService(IWebDataServiceConfiguration config)
    {
        config.SetResourceContainerAccessRule("Customers", ResourceContainerRights.AllRead);
        config.SetServiceOperationAccessRule("CustomersInLondon", ServiceOperationRights.All);
    }
    
    [WebGet]
    public IQueryable<Model.Customers> CustomersInLondon()
    {
        return from c in this.CurrentDataSource.Customers
               where c.City == "London"
               select c;
    }  
}

The IQueryable<T> is composable, so you can filter the result with a URL, such as: http://localhost/WebDataService1.svc/CustomersInLondon('AROUT')/Orders. If you want to prevent users from modifying the query, change IQueryable<Model.Customers> to IEnumerable<Model.Customers>.

Marcelo also explains how substituting [WebInvoke] for [WebGet] lets you pass parameters in the request body instead of the URL, [SingleResult] indicates that only a single instance is returned, and [MimeTypeAttribute] applies a particular MIME type, such as text/html, to the returned element(s).

Danny Simmons Describes a Serializable EntityBag for n-Tier Entity Framework Deployment

I'm glad to see the ADO.NET Entity Framework team taking the pragmatic approach to Web service-enabling v1.0 with the serializable EntiyBag that Danny describes in his EntityBag Part I – Goals post of January 20, 2008.

For my take on Danny's answer to the LINQ to SQL Team's apparently-abandoned serializable "mini-connectionless DataContext" read Coming to the Entity Framework: A Serializable EntityBag for n-Tier Deployment, which ran too long for inclusion in this post.

[Repeated from the previous week's post.]

Jim Wooley's ThinqLinq Site is Live and LINQ In Action Has Gone to the Printer

Over the weekend Jim's new LINQ-powered ThinqLinq blog and site added new content and Manning Publications Co. sent LINQ in Action by Fabrice Marguerie, Steve Eichert and Jim Wooley to the printer. Copies should be in bookstores and Amazon.com by mid-February.

Congratulations to Fabrice, Steve and Jim and best wishes for the book's success!

I have all the LINQ books currently published, including the eBook version of LINQ in Action and I agree with Sam Gentile comment in his post of January 22, 2008:

My best of friends, Steve Eichert, along with  Fabrice, Jim, has shipped their LINQ in Action. I can tell you that I have the LINQ books, and this is by far the best.

Take Advantage of the LinqDataSource's GroupBy and OrderGrouopBy Features with Nested ListView Controls

Matt Berseth's Building a Grouping Grid with the ASP.NET 3.5 LinqDataSource and ListView Controls post of January 10, 2008 shows you how to create an expandable master/child ListView presentation that doesn't require a postback for expanding or collapsing. The preceding link is to a live demo and the source code is available to download.

Matt's earlier Creating a FrequencyDecoder.com Style Grid with the ListView, DataPager and LinqDataSource Controls post shows you how to create a soothingly attractive, customized pastel grid with a single ListView control.

Added 1/23/2008: Missed in previous linkblog post.

6 comments:

Unknown said...

Thanks Roger! We worked long and hard on getting it to be the best it could be and it's rewarding to hear such positive feedback!

Anonymous said...

Roger, I first posted that the EDMX CodeGeneration sample was from Asad, but I was wrong. Thanks.

Anonymous said...

To answer your question, with "LINQ to Relational" we designate a group of LINQ implementations that provide access to data stored in relational databases. It is not really the name of a specific product.

Anonymous said...

Roger
I definitely favor wshttp because all of the data (original and current) are stored in the snapshot that is coming over the wire.

Julie

Roger Jennings (--rj) said...

@Diego,

I've edited the topic.

Thanks for the update,

--rj

Roger Jennings (--rj) said...

@Julie,

The way I read the tea leaves, basicHttpBinding lets you easily enable or disable WS-* security; the default is disabled. wsHttpBinding adds transactions, reliable messaging, and WS-Addressing.

However, I don't believe that the binding type alters the message content at all. Only the wire format changes, AFAIK.

--rj