Environment: Net Core 3.1 Web Api, Microsoft.AspNetCore.OData 7.4.0-beta
I've an OData api controller CustomersController that it's working fine, but $expand always returns NullReferenceException. Customer has a one to many Contacts and Addresses.
This is the code of action method:
[HttpGet]
[ODataRoute]
[EnableQuery]
public IActionResult Get()
{
try
{
var customers = _context.Customers;
_logger.LogInfo($"Returned {customers.Count()} customers from database.");
return Ok(customers);
}
catch (Exception ex)
{
_logger.LogError($"Something went wrong inside Get customers action: {ex.Message}");
return StatusCode(500, "Internal server error");
}
}
And this is the customer object definition:
public class Customer : BaseEntity
{
public Guid CustomerId { get; set; }
[Required(ErrorMessage = "Name is required")]
[StringLength(100, ErrorMessage = "Name can't be longer than 100 characters")]
public string Name { get; set; }
[StringLength(100, ErrorMessage = "Commercial name can't be longer than 100 characters")]
public string CommercialName { get; set; }
[Required(ErrorMessage = "Tax code is required")]
[StringLength(25, ErrorMessage = "Tax code can't be longer than 25 characters")]
public string TaxCode { get; set; }
[StringLength(250, ErrorMessage = "Email can't be longer than 250 characters")]
[EmailAddress]
public string Email { get; set; }
[StringLength(250, ErrorMessage = "Url can't be longer than 250 characters")]
public string Url { get; set; }
public ICollection<Contact> Contacts { get; set; }
public ICollection<Address> Addresses { get; set; }
public ICollection<Work> Works { get; set; }
}
DbContext configuration:
// Table
builder.ToTable("Customers");
builder.HasKey(b => b.CustomerId);
// Relationships
builder.HasMany(b => b.Contacts)
.WithOne(b => b.Customer)
.HasForeignKey(b => b.CustomerId)
.OnDelete(DeleteBehavior.Cascade);
builder.HasMany(b => b.Addresses)
.WithOne(b => b.Customer)
.HasForeignKey(b => b.CustomerId)
.OnDelete(DeleteBehavior.Cascade);
I have another controller but with one to one relationships and this controller works fine with $expand with the same code in action method.
Any idea about what is failing? Regards