The World’s Leading Microsoft .NET Magazine
   
 
The .NET Addict's Blog

My Top Tags

                                                           

My RSS Feeds








Latest Diggs - Programming

Internet Blogs - Blog Top Sites

Site Hits

Total: 2,639,104
since: 19 Jan 2005

Exposing LINQ to SQL entities through a POX service in VS 2008

posted Thu 23 Aug 07

I recently got to dig into LINQ to SQL for a little while. I'd been spending a lot of time with Astoria and LINQ to Entities, but as we know - Astoria and the Entity Framework don't currently work on VS 2008 Beta 2, so the only choice left when Astoria is missing is to write an .asmx service decorated with the [ScriptService] tag so that it exposes itself via raw XML (POX) instead of SOAP.

Everything in LINQ to SQL works splendidly - I defined my database, then I added a .dbml file to my project, dragged a couple tables onto the designer surface and built and I was ready to query - no worries. Then I tried to return an array of LINQ to SQL entities by calling .ToArray() on my results query. It would have worked fine, but there's a gotcha - if you happen to have a one-to-many relationship that, on the other side, is a one-to-one - you're going to run into a circular reference error and your results will fail to serialize into XML.

For instance, let's say you have a table called Products. Each product has a single Category (which is indicated by the CategoryID column). There's a right and proper foreign key relationship there, and assuming pluralization is working right - you're going to get a Product class with a Category property. This is fine. The Category class is where the problem occurs, because this class is, by default, going to come with a Products property that refers to all products in that category. While potentially handy, this is the source of the circular reference. To turn that property off, you just go into your data model designer and set "Child Property" on the Category table/class to "false". Now it will not generate a Products property and you can serialize the entire object graph without a problem.

If, at some point, you want to serialize all products that belong to a given category, you can just run a LINQ query against the products table and filter on Category.CategoryName or CategoryID - the property for walking backwards up the tree is not necessary in this case and it makes serialization impossible.

So, hopefully this little tip saves someone a couple hours of headache!

tags:              

links: digg this    del.icio.us    technorati    reddit




Tag Related Posts

MobileMe vs. Live Mesh - Round 1

Wed 11 Jun 08 12:20 A GMT-05

C# 3.0 - Are Object Initializers Evil, Useful, or Both?

Fri 26 Oct 07 10:53 A GMT-05
tags:    

Continuous LINQ - Can I write games with it?

Mon 13 Aug 07 3:09 P GMT-05
tags:        

Silverlight 1.1 Alpha Refresh

Fri 10 Aug 07 2:09 P GMT-05
tags:    

Orcas Beta 2 - might as well be a CTP

Tue 07 Aug 07 1:15 A GMT-05
tags:  

Dynamic, Observable LINQ Views

Tue 31 Jul 07 1:21 A GMT-05
tags:        

Acropolis or Acrapolis?

Thu 05 Jul 07 12:34 P GMT-05
tags:      

My first "Acropolis" Application

Mon 04 Jun 07 1:40 P GMT-05
tags:      

Orcas' Hidden Gem - The managed PNRP stack

Fri 11 May 07 6:45 P GMT-05
tags:        

Orcas EDM Wizard Patched

Fri 27 Apr 07 11:56 A GMT-05
tags:      

Installing Orcas Beta 1 - VMware Style

Mon 23 Apr 07 12:16 P GMT-05

Orcas Beta 1 Released

Fri 20 Apr 07 7:09 P GMT-05

Visual Studio "Orcas" - March CTP is Available

Wed 28 Feb 07 12:28 P GMT-05
tags:            

Objective-C Categories vs C# 3.5 Language Extensions

Mon 26 Feb 07 1:05 P GMT-05
tags:                

What I think is a bug in WCF POX messaging

Thu 04 Jan 07 4:58 P GMT-05
tags:      

ADO.NET Entity Framework Announced Today!

Wed 16 Aug 06 11:08 A GMT-05

DLinq vs the ADO.NET Entity Framework

Fri 23 Jun 06 4:01 P GMT-05

WPF/XAML and LINQ - A match made in heaven

Tue 06 Jun 06 11:32 A GMT-05
tags: