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

Exporting jars with resources

java eclipse jar

I'm trying to build a large Java project by building many small, modular projects. I want each small project to be entirely self contained, holding all of its resources (such as images).

Push to Heroku No Changes

ruby-on-rails git heroku push

I have been able to successfully push my files earlier in the day with changes showing up. However now when I have made changes, and done the push the changes don't show up on Heroku, despite thee

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.