Detaching EF entities from DB context, removing their relations?

    public List<PRODUCT> GetProductsByProductcategory(decimal productCategoryKey)
    {
        DBEntities ctx = new DBEntities();
        List<PRODUCT> productList = ctx.PRODUCT.Where(p => p.PRODUCT_CATEGORY_KEY == productCategoryKey).ToList();
        return productList;
    }

Is there any way to detach each PRODUCT-entity from the ctx instance, thus removing their relations to other entities? Or any other smart way to remove the related entities. My problem is that i cant serialize entities with relations. Its resulting in a circular reference exception. My current solution is to use ctx.ContextOptions.ProxyCreationEnabled = false which works. But if i do it like that i wont get any lazy loading. Help is much appreciated. Thanks

Answers


Since you are serializing to JSON, I would recommend using the Json.NET library from James Newton-King. You can turn off the relations from being serialized. Below is the serialization method that I am using with the ReferenceLooping turned off.

 public static string Serialize(object model)
 {
     var settings = new JsonSerializerSettings
                     {
                        ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                     };
     return JsonConvert.SerializeObject(model, Formatting.None, settings);
 }

Need Your Help

creating a thumbnail image from video by ffmpeg

php ffmpeg thumbnails

$video_name = $_FILES['video_file']['name'];

How to change color in RGB format in FTGL OpenGL?

c++ opengl ftgl

How to change color in RGB format in FTGL OpenGL?