Last week Telerik released the Data Service Wizard Beta 1. It will automatically create for you the end points for an Astoria, WCF, or RESTful service. New in the beta of the Data Service Wizard is the ability of the wizard to automatically generate the DataServiceKey attribute required to make relationships in Astoria work.

When you use "Astoria" (ADO.NET||WCF) Data Services, by default Astoria tries to map the primary keys in your entities using a convention. This is important for your service to work. The mapping works out of the box for the Entity Framework, however, if you are using LINQ to SQL or Telerik Open Access, it does not since some of your tables may have a primary key that will not map to the CLR primitive types that follow the Astoria convention for key mapping. (Order Details in Northwind bombs for example since both of its composite key are entities and not primitive CLR types.)

There is a very simple fix for this. You have to make your entity a partial class and then decorate the entity using the DataServiceKey attribute, in the constructor. Recently we added support for this in the Data Service Wizard: by default we do this for you by adding a “DalDataServiceKeys.cs“ (or VB) file to your data access layer project automatically.

image

The code is show below for our DalDataServiceKeys.cs file shown in the Telerik.OA.DAL project above. You will notice on Line 36 we will even convert the complex type to a primitive CLR type so Astoria can handle it.

 1: namespace Telerik.OA.DAL
 2: {
 3:  using System.Data.Services.Common;
 4:  
 5:  /// <summary>
 6:  /// Category Class Data Service
Key Fix
 7:  /// </summary>
 8:  [DataServiceKey("CategoryID")]
 9:  public partial class Category
 10:  {
 11:  }
 12:  /// <summary>
 13:  /// Customer Class
Data Service Key Fix
 14:  /// </summary>
 15:  [DataServiceKey("CustomerID")]
 16:  public partial class Customer
 17:  {
 18:  }
 19:  /// <summary>
 20:  /// Employee Class Data Service
Key Fix
 21:  /// </summary>
 22:  [DataServiceKey("EmployeeID")]
 23:  public partial class Employee
 24:  {
 25:  }
 26:  /// <summary>
 27:  /// Order Class
Data Service Key Fix
 28:  /// </summary>
 29:  [DataServiceKey("OrderID")]
 30:  public partial class Order
 31:  {
 32:  }
 33:  /// <summary>
 34:  /// OrderDetail Class Data
Service Key Fix
 35:  /// </summary>
 36:  [DataServiceKey(new string[]{"OrderID","ProductID"})]
 37:  public partial class OrderDetail
 38:  {
 39:  }
 40:  /// <summary>
 41:  /// Product Class
Data Service Key Fix
 42:  /// </summary>
 43:  [DataServiceKey("ProductID")]
 44:  public partial class Product
 45:  {
 46:  }
 47:  /// <summary>
 48:  /// Region Class Data Service
Key Fix
 49:  /// </summary>
 50:  [DataServiceKey("RegionID")]
 51:  public partial class Region
 52:  {
 53:  }
 54:  /// <summary>
 55:  /// Shipper Class
Data Service Key Fix
 56:  /// </summary>
 57:  [DataServiceKey("ShipperID")]
 58:  public partial class Shipper
 59:  {
 60:  }
 61:  /// <summary>
 62:  /// Supplier Class Data Service
Key Fix
 63:  /// </summary>
 64:  [DataServiceKey("SupplierID")]
 65:  public partial class Supplier
 66:  {
 67:  }
 68:  /// <summary>
 69:  /// Territory Class
Data Service Key Fix
 70:  /// </summary>
 71:  [DataServiceKey("TerritoryID")]
 72:  public partial class Territory
 73:  {
 74:  }
 75: }

This will enable you to use Astoria with OpenAccess for all of the tables in your database. I converted my Tech*Ed “Data Access Hacks and Shortcuts” session demo to use OpenAccess and Astoria from the Entity Framework in less than 5 minutes. (I will show it and give away the code on my blog in a week or two.)

image

Enjoy!

Technorati Tags: ,

About the Author

Steve Forte

 sits on the board of several start-ups including Triton Works. Stephen is also the Microsoft Regional Director for the NY Metro region and speaks regularly at industry conferences around the world. He has written several books on application and database development including Programming SQL Server 2008 (MS Press).

Comments

Comments are disabled in preview mode.