Exception while trying to deserialize JSON into EntityFramework using JavaScriptSerializer
        Posted  
        
            by 
                Barak
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Barak
        
        
        
        Published on 2010-07-14T19:35:39Z
        Indexed on 
            2010/12/26
            12:53 UTC
        
        
        Read the original article
        Hit count: 330
        
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.List
1[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.
© Stack Overflow or respective owner