I am building a ASP.NET webapplication and I am also working with Entity Framework Code First. In this case I am using two entities (Customer and Contact) which have a One-to-Many relation with eachother (one Customer can have multiple Contacts). While getting the data from the database, everything goes fine. I also use Viewmodels, so next to my data entities, I also have two models called CustomerModel and ContactModel.
Below here I will show my entities and my viewmodels:
Customer-entity
[Table("Customer")]
public class Customer
{
[Key()]
public Guid Id { get; set; }
//other non-relevant properties
public virtual List<Contact> Contacts { get; set; }
}
Contact-entity
[Table("Contact")]
public class Contact
{
[Key()]
public Guid Id { get; set; }
//non-relevant properties
[ForeignKey("Customer")]
public Guid CustomerId { get; set; }
[Required]
public virtual Customer Customer { get; set; }
}
CustomerModel
[Serializable]
public class CustomerModel
{
public Guid Id { get; set; }
//non-relevant properties
[ScriptIgnore(ApplyToOverrides = true)]
public virtual List<ContactModel> Contacts { get; set; }
}
ContactModel
[Serializable]
public class ContactModel
{
public Guid Id { get; set; }
//non-relevant properties
public Guid CustomerId { get; set; }
[ScriptIgnore(ApplyToOverrides = true)]
public virtual CustomerModel Customer { get; set; }
}
When I run my application and the code as well, it all works fine in the backend until it needs to serialize it to JSON in my HomeController.
public ActionResult GetCustomerById(Guid id)
{
CustomerModel customer = new CustomerManager().GetById(id);
string output = serializer.Serialize(customer);
return Content(output);
}
Even the CustomerModel object gets the 2 contacts, but at my Serialize(customer) method, it doesn't parse it to JSON. So actually, when I debug it and look at my output, I see every property but not the nested ContactModels.
In my case the string output doesn't contain the two Contacts. How do I solve this? I have checked some 'similar' questions on Stackoverflow as well but with no result.
CustomerModelandContactModelare view models? If they were, they would not have[Serializable]or[ScriptIgnore]attributes and the properties would not be virtual. Apart from the circular reference issue (removeCustomerModel Customer), if you want to return Json, the usereturn Json(customer);