locked
WCF Data Services and Repository Pattern RRS feed

  • Question

  • Hello,

    I am getting this nasty error when I try to run my project:

     

    The server encountered an error processing the request. The exception message is 'The property 'Attachments' on type 'DataAccessLayer.CaseReview.Case' is not a valid property. Properties whose types are collection of primitives or complex types are not supported.'. See server logs for more details. The exception stack trace is:

    at System.Data.Services.Providers.ReflectionServiceProvider.BuildTypeProperties(ResourceType parentResourceType, IDictionary`2 knownTypes, IDictionary`2 childTypes, Queue`1 unvisitedTypes, IEnumerable`1 entitySets) at System.Data.Services.Providers.ReflectionServiceProvider.PopulateMetadataForTypes(IDictionary`2 knownTypes, IDictionary`2 childTypes, Queue`1 unvisitedTypes, IEnumerable`1 entitySets) at System.Data.Services.Providers.ReflectionServiceProvider.PopulateMetadata(IDictionary`2 knownTypes, IDictionary`2 childTypes, IDictionary`2 entitySets) at System.Data.Services.Providers.BaseServiceProvider.PopulateMetadata() at System.Data.Services.DataService`1.CreateProvider() at System.Data.Services.DataService`1.HandleRequest() at System.Data.Services.DataService`1.ProcessRequestForMessage(Stream messageBody) at SyncInvokeProcessRequestForMessage(Object , Object[] , Object[] ) at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

     

    Here is the explanation of our environment:

    We are using the Repository pattern with EF4 in a DataAccessLayer project. Case is a entity which has a one-to-many relationship to the Attachments entities. This problem occurs because 'Attachments' is a EntityCollection property of class Case. (Automatically Generated using the ADO.NET Entity Data Model)

    So essentially, I have a similar problem as this thread but I don't know what is the best way to address it. (I don't have anywhere to add the IgnoreProperties since I am not using POCO objects)

    Wednesday, October 27, 2010 5:42 PM

Answers

  • If anyone is interested. In my experience it's utterly impossible to use WCF Data Services with the code generated entity objects which are created if you're using a repository pattern which is tied into an ADO.NET Entity Data Model. I tried creating partial classes and applying the IgnoreProperties but after ignoring enough properties, it started complaining about EntityValueKeys which is in the System namespace.

    The Solution:

    After going through many different scenarios, I ended up going the POCO route for my Entities and then creating a DbContext  class which exposes the entities to my repositories. I essentially followed the example outlined here:

    http://blogs.msdn.com/b/efdesign/archive/2010/06/21/productivity-improvements-for-the-entity-framework.aspx

     

    A few notes:

    If you're going to use the code-first approach with an existing database, you're going to get an error saying something along the lines of "your model has changed". It has to do with how the DbContext is initializing the Database. In order to prevent this error you will have to add Database.SetInitializer<YourDbContextClass>(null); before you're going to be using the "YourDbContextClass" for fetching data.

    Also, in "YourDbContextClass" constructor, add the following to make sure that the serialization happens correctly:

    ObjectContext.ContextOptions.ProxyCreationEnabled = false;

     

    • Marked as answer by DeviantSeev Thursday, October 28, 2010 3:33 PM
    Thursday, October 28, 2010 3:33 PM