views:

18

answers:

1

I'm trying to deserialize JSON which I'm getting from an external source into an Entity Framework entity class using the following code:

var serializer = new JavaScriptSerializer();
IList<Feature> obj = serializer.Deserialize<IList<Feature>>(json);

The following exception is thrown:

Object of type 'System.Collections.Generic.List1[JustTime.Task]' cannot be converted to type 'System.Data.Objects.DataClasses.EntityCollection1[JustTime.Task]'.

My model is simple: The Feature class has a one-to-many relation to the Tasks class. The problem appears to be the deserializer is trying to create a generic List to hold the collection of tasks instead of an EntityCollection.

I've tried implementing a JavaScriptConverted which would handle System.Collections.Generic.List but it didn't get called by the deserializer.

A: 

You can't assign a list directly to an EF collection. You have to add to it. So deserialize to a POCO and then copy.

Craig Stuntz
This is obviously the easy way out but would require me to duplicate the entire object model just to circumvent a minor technical issue, not really my preferred method of doing things.
Barak