1

I'm using ASP.NET Web API with Entity Framework but i'm facing a problem while generating the JSON for the navigation property:

I have two tables; Product and Category.

    public class Product
        {
            public int ProductID { get; set; }
            public string ProductName { get; set; }

            public virtual Category Category { get; set; }
        }

public class Category
    {    
        public int CategoryID { get; set; }
        public string CategoryName { get; set; }

        public virtual ICollection<Product> Products { get; set; }
    }

when I generate the JSON for Product it generates the JSON category which is fine but inside the Category JSON there is another JSON pointing to JSON product so a huge JSON file is create i tried to solve this issue by removing virtual but every time i update the model i face the same problem. is there any way to solve?

1 Answer 1

4

If you are using Newtonsoft.Json, then you can simply apply the attribute [JsonIgnore] to the properties you wish to ignore.

public class Category
{    
    public int CategoryID { get; set; }
    public string CategoryName { get; set; }

    [JsonIgnore]
    public virtual ICollection<Product> Products { get; set; }
}

With this setup, whenever category is serialized to Json, it will ignore the Products collection

Sign up to request clarification or add additional context in comments.

1 Comment

JsonIgnore Attribute goes away on EDMX change or update.. How do preserve it ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.