1

I'm new to asp.net MVC4 and I have an issue with the serialization of an entire Dataset.

When I return this Dataset, eg. db.Prestations.ToList() and call my endpoint in Postman, the request takes much time and doesn't respond.

If I put the result of db.Prestations.ToList() in a variable and I throw an exception after, I get the exception in my request.

So it's seems to be a serialization issue, like the data returned are too big.

My question is how do I remove sub-objects I don't need in my Prestations ?

Here is my model, and I don't want it to return the three Hashsets, how can I do that ?

namespace Uphair.EfModel
{
using System;
using System.Collections.Generic;

public partial class Prestation
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Prestation()
    {
        this.PartenairePrestations = new HashSet<PartenairePrestation>();
        this.PrixDureeOptions = new HashSet<PrixDureeOption>();
        this.LigneReservations = new HashSet<LigneReservation>();
    }

    public int IdPrestation { get; set; }
    public string NomPrestation { get; set; }
    public int Categorie { get; set; }
    public Nullable<int> CoifEsthe { get; set; }
    public Nullable<int> IdPrestationCategorie { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<PartenairePrestation> PartenairePrestations { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<PrixDureeOption> PrixDureeOptions { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<LigneReservation> LigneReservations { get; set; }
    public virtual PrestationCategorie PrestationCategorie { get; set; }
}
}

Thanks to anyone who will take the time to help me :)

Pierrick Martellière
  • 1,554
  • 3
  • 21
  • 42

1 Answers1

1

You can use JsonIgnoreAttribute and DataMemberAttribute

By default Json library will include all of a classes public properties and fields in the JSON it creates. Adding the JsonIgnoreAttribute to a property tells the serializer to always skip writing it to the JSON result.

[JsonIgnore]
public int Categorie { get; set; }

If you only want to serialize a small subset of class properties then the best way to tackle this scenario is to add the DataContractAttribute to the class and DataMemberAttributes to the properties to serialize. This is opt-in serialization, only the properties you mark up with be serialized, compared to opt-out serialization using JsonIgnoreAttribute.

[DataContract]
public class Prestation
{  
  // included in JSON
  [DataMember]
  public int IdPrestation { get; set; }
  [DataMember]
  public string NomPrestation { get; set; }

  //ignored in JSON
  public int Categorie { get; set; }
  public Nullable<int> CoifEsthe { get; set; }
  public Nullable<int> IdPrestationCategorie { get; set; }
}
Ivan Salo
  • 811
  • 1
  • 9
  • 25