Dynamic Data and Entity Framework

It's been a bit quiet around here recently. I picked up some sort of chest infection, was confined to bed for a few days and still have the nasty cough to prove I'm not well. I thought I'd throw up a few posts about stuff I'd stumbled across in my incoherent state.

Firstly, Eric phoned me to ask a few questions about Dynamic Data and was having problems using it with Entity Framework. Specifically, he was running into issues with certain tables in the Northwind database (this reminds me of an issue with Linq to Sql and ADO.NET Data Services, we'll come back to that one). It turns out that the RTM release of Dynamic Data has trouble with certain types of relationship in EF data models so you might see something like this when you navigate to, for example, the Order_Details table:

System.Web.HttpException was unhandled by user code
  Message="DataBinding: 'System.Web.UI.WebControls.EntityDataSourceWrapper' does not contain a property with the name 'Orders.OrderID'."
  Source="System.Web"
  ErrorCode=-2147467259
  StackTrace:
       at System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName)
       at System.Web.DynamicData.ModelProviders.TableProvider.EvaluateForeignKey(Object row, String foreignKeyName)
       at System.Web.DynamicData.MetaForeignKeyColumn.GetForeignKeyValues(Object row)
       at System.Web.DynamicData.MetaForeignKeyColumn.GetForeignKeyPath(String action, Object row, String path)
       at System.Web.DynamicData.FieldTemplateUserControl.get_ForeignKeyPath()
       at ForeignKeyField.GetNavigateUrl() in c:\Users\mormond\Desktop\WebSite1\DynamicData\FieldTemplates\ForeignKey.ascx.cs:line 48
       at ASP.dynamicdata_fieldtemplates_foreignkey_ascx.__DataBindingHyperLink1(Object sender, EventArgs e) in c:\Users\mormond\Desktop\WebSite1\DynamicData\FieldTemplates\ForeignKey.ascx:line 3
       at System.Web.UI.Control.OnDataBinding(EventArgs e)
       at System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding)
       at System.Web.UI.Control.DataBind()
       at System.Web.UI.Control.DataBindChildren()

Or something like this if you navigate the Product -> Order_Details relationship:

 [InvalidOperationException: A property with name 'Products.ProductID' does not exist in metadata for entity type 'NorthwindModel.Order_Details'.]
   System.Web.UI.WebControls.EntityDataSourceView.GenerateWhereClause(String& whereClause, ObjectParameter[]& whereParameters) +919
   System.Web.UI.WebControls.EntityDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments, Creator qbConstructor) +306
   System.Web.UI.WebControls.EntityDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +154
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +19
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +142
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73
   System.Web.UI.WebControls.GridView.DataBind() +4
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
   System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +72
   System.Web.UI.Control.EnsureChildControls() +87
   System.Web.UI.Control.PreRenderRecursiveInternal() +44
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842

There's a fix posted on the CodePlex site which involves replacing the EF Data Model provider with a new version to workaround the problem. Full instructions are on the site but it boils down to including the new DLL in your bin folder (or adding a reference for a Web Application) and updating the call to model.RegisterContext() in global.asax.

Technorati Tags: asp.net,dynamic data,entity framework