6

I've manay-to-many relationship between two entities: Categories <--> Items

public class CategoryMaster
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual List<SubCategoryMaster> SubCategories { get; set; }

    public List<ItemMaster> Items { get; set; } 
}

public class ItemMaster
{
    public long Id { get; set; }
    public string Name { get; set; }

    public List<CategoryMaster> Categories { get; set; }
}

Whenever I try to explicit load related items to all/certain categories, it gives me

  • all related items
  • related categories to those items
  • related items to those categories and so on...nested/circular references

       db.CategoryMaster
            .Include(x=>x.Items)
            .Include(x=>x.SubCategories.Select(y=>y.Items))
            .ToList();
    

Hence results in below error while serializing it to JSON on *.cshtml with Json.Encode();

A circular reference was detected while serializing an object of type 'GoGreen.Data.Entities.SubCategoryMaster'.

Since I've disabled the lazy loading at property level, I'm not expecting it to load all nested entities(circular references) at any point of time. Is there a way to load all related level one records i.e. Categories and related items.

Related question - But Iodon't want to go with any of the two ways suggested.

NOTE : I'm more interested in knowing why EF behaves like this. It seems a bug to me.

Community
  • 1
  • 1
RollerCosta
  • 5,020
  • 9
  • 53
  • 71

3 Answers3

0

First approach: you can add attribute above properties you don't want to exclude it from being serialized using [ScriptIgnore], you can create partial class and add your customization if your entities are auto generated

Second approach: Create a Model with only properties you need in your view and select only this model and set your properties

EFcontext.Tabel.include(x=>x...).Select(x=>new MyModel { ... });
Ahmed
  • 1,542
  • 2
  • 13
  • 21
  • There is nothing wrong with this approach, but I won't prefer doing this as I'm expecting some fix in EF framework itself. – RollerCosta Dec 25 '16 at 15:26
0

One workaround, and please don't kill me :-) After object loading and before serializing, just set the loaded objects which are causing the circular reference to null. I tried it and worked like a charm.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
0

use meta data redirection. figured I would help anyone who stumbled here.

[MetadataType(typeof(CategoryMasterMetadata))]
public partial class CategoryMaster
{
}


public class CategoryMasterMetadata
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }

    [JsonIgnore]
    public virtual List<SubCategoryMaster> SubCategories { get; set; }

    public List<ItemMaster> Items { get; set; } 
}