I am building a net core stack and implementing the layers as async.
I have the following customer repository code that is called by a web api layer
public class CustomerRepository : ICustomerRepository
{
//interface this up
private DbContextOptionsBuilder<SykesTestContext> optionsBuilder = new DbContextOptionsBuilder<SykesTestContext>();
public CustomerRepository(string connection)
{
optionsBuilder.UseSqlServer(connection);
}
public async Task<List<Dom.Customer>> GetAll()
{
List<Dom.Customer> customers = new List<Dom.Customer>();
var efCustomers = new List<Ef.Customer>();
using (var customerContext = new TestContext(optionsBuilder.Options))
{
efCustomers = await customerContext.Customer
.Include(cust => cust.Address)
.Include(cust => cust.Telephone)
.ToListAsync();
foreach (var customer in efCustomers)
{
customers.Add(Mapper.Map<Ef.Customer, Dom.Customer>(customer));
}
}
return customers;
}
}
THE API Calling method
[Produces("application/json")]
[Route("api/Customers")]
public class CustomersController : Controller
{
ICustomerRepository _customerRepository;
public CustomersController(ICustomerRepository customerRepository)
{
_customerRepository = customerRepository;
}
public async Task<List<Customer>> Get()
{
try
{
var customers = await _customerRepository.GetAll();
return customers;
}
catch (Exception ex)
{
throw ex;
}
}
}
If I comment out the "includes" then it all works fine.
If I use the includes (to build the object child collections) I get the following error in the browser: net::ERR_CONNECTION_RESET and only part of the json object is returned?
Any ideas?
public partial class Customer
{
public Customer()
{
Address = new HashSet<Address>();
Telephone = new HashSet<Telephone>();
}
public int Id { get; set; }
public int ClientId { get; set; }
public int TypeId { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public int? ExternalId { get; set; }
public virtual ICollection<Address> Address { get; set; }
public virtual ICollection<Telephone> Telephone { get; set; }
}
Domain Model
public class Customer
{
public int Id { get; set; }
public int ClientId { get; set; }
public int TypeId { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public int? ExternalId { get; set; }
public virtual ICollection<Address> Address { get; set; }
public virtual ICollection<Telephone> Telephone { get; set; }
}