My env is Asp.Net Core 2.1 EF Core 2.1
public class Customer
{
public int Id { get; set; }
public string name { get; set; }
public virtual ICollection<CustomerLocation> CustomerLocations { get; set; }
public class CustomerLocation
{
public int Id { get; set; }
public int customerId { get; set; }
public string streetAddress { get; set; }
public string zipCode { get; set; }
public string city { get; set; }
public string state { get; set; }
public string category { get; set; }
public virtual Customer Customer { get; set; }
}
In my Api controller
// GET: api/Customers
[HttpGet]
public IEnumerable<Customer> GetCustomers()
{
var custlist = _context.Customers
.Include(c=>c.CustomerLocations)
.ToList();
return custlist;
}
and I would like to receive this JSON
[
{
id: 1,
name: "My First Company",
customerLocations: [
{
id: 1,
customerId: 1,
streetAddress: "13 Union Street",
zipCode: "94111",
city: "San Francisco",
state: "CA",
category: "Headquarter",
customer: null
},
{
id: 2,
customerId: 1,
streetAddress: "1098 Harrison St",
zipCode: "94103",
city: "San Francisco",
state: "CA",
category: "Warehouse",
customer: null
}]
},
{
id: 2,
name: "Another Company",
customerLocations: [ ]
}
]
but the answer i receive is
[
{
id: 1,
name: "My First Company",
customerLocations: [
{
id: 1,
customerId: 1,
streetAddress: "13 Union Street",
zipCode: "94111",
city: "San Francisco",
state: "CA",
category: "Headquarter"
then it crashes trying to loop into the "customer" navigation property of "customerLocation".
The only way I found to get rid of this is to explicitly null all "customer" references in each CustomerLocation, but I can't believe this is the correct way to deal with this.