To get around this I converted my Entities to POCO based Code First. To do this right click inside your edmx window and select:
Add code generation item > Code tab > EF POCO Entity Generator.
Note that you might need to install it with nuget if you don't see it.
At runtime however EF adds proxy classes to those objects for tracking purposes but they tend to mess up with the serialization process. To prevent this we can simply set ProxyCreationEnabled to false as follows:
var context = new YourEntities();
context.Configuration.ProxyCreationEnabled = false;
var results = context.YourEntity.Take(100).ToList();
You can then safely return JSON.NET serialized data by omitting the default reference looping as follows:
return JsonConvert.SerializeObject(results, Formatting.Indented,
new JsonSerializerSettings {
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…