2

There are many related entities in Domain assembly. For example People that has navigation properties (Level1) to FamilyRelations, Houses and Persons. Beside this the Houses has own nav.prop (Level2) to Address and Address (Level3) has to City, Street ... etc. enter image description here When I set LazyLoadingEnabled to true then I'm getting JSON (on the left side in screen) with all related entities.

How can I get only one level of nesting (as on the right side in scree) or set other levels to NULL value (because I had setting Newtonsoft.Json.NullValueHandling.Ignore)?
Can I implement it without use .Include to each entity?

My class of People:

    public class People : BaseEntity
    {
        public int PersonID { get; set; }

        public int HouseID { get; set; }

        public int PeopleNumber { get; set; }

        public int? FamilyRelationID { get; set; }            

        //FK to House
        public virtual House Houses { get; set; }
        //FK to Person
        public virtual Person Persons { get; set; }
        //FK to FamilyRelations
        public virtual FamilyRelations FamilyRelations { get; set; }
    }

WebAPI config:

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new   MediaTypeHeaderValue("text/html"));
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling
            = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
config.Formatters.JsonFormatter.SerializerSettings.NullValueHandling
             = Newtonsoft.Json.NullValueHandling.Ignore;

I do not have any solution because I did not have enough experience with it.
So, I need your suggestions, advices about it. Sorry for my English and if I have to add more informations, please let me know. Thanks

UPDATE
I've tried to add [JsonIgnore] or ignore those properties in mapping class but when I do request get/House then I need to get field from Address without nav.prop and when request get/People then I do not nedd Address. As a result I can't ingnore it.

Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116

2 Answers2

1

Never return tracked objects to the controller. Your business logic code (which should not exist in the controller) should map your database aware objects to POCOs. This can be as simple as using

var poco = AutoMapper.Map<People>(livePerson)

And you setup in your mapping profile to ignore those properties so they're not copied.

Note my automapper-fu is rusty that syntax is rough code.

You want to be very careful with any blind mapping as it opens you up to the Mass Assignment vulnerability. This is equally true for going straight to your live tracked objects. If a user sees in their data IsAdmin: false, they might get crafty and post IsAdmin: true. This can be saved to your database with blind assignments.

Chris Marisic
  • 32,487
  • 24
  • 164
  • 258
  • Thanks. Could you please explain to me how should I manage what properties need to ignore in cases when I want to get `People` and `House`? When `People` then need to ignore `Address` of `House` but when `House` then I do not need to ignore `Address`. Sorry if it not clear – Roman Marusyk Sep 17 '15 at 09:21
  • You setup different maps for each situation. – Chris Marisic Sep 17 '15 at 17:13
0

If you are look for a way to ignore navigation properties in json serialization, this answer can help and you can ignore navigation properties in json serialization by it.

Community
  • 1
  • 1
Ramin Bateni
  • 16,499
  • 9
  • 69
  • 98