开发者

Exception while trying to deserialize JSON into EntityFramework using JavaScriptSerializer

I'm trying to des开发者_StackOverflowerialize 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜