OData Web Api控制器中带有$expand的NullReferenceException
环境:Net Core 3.1 Web Api,Microsoft.AspNetCore.OData 7.4.0-beta 我有一个ODataAPI控制器CustomerController,它工作正常,但$expand总是返回NullReferenceException。客户有一对多的联系人和地址 这是行动守则方法:OData Web Api控制器中带有$expand的NullReferenceException,odata,asp.net-core-webapi,nullreferenceexception,asp.net-core-3.1,Odata,Asp.net Core Webapi,Nullreferenceexception,Asp.net Core 3.1,环境:Net Core 3.1 Web Api,Microsoft.AspNetCore.OData 7.4.0-beta 我有一个ODataAPI控制器CustomerController,它工作正常,但$expand总是返回NullReferenceException。客户有一对多的联系人和地址 这是行动守则方法: [HttpGet] [ODataRoute] [EnableQuery] public IActionResult Get() {
[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");
}
}
这是客户对象定义:
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; }
}
我有另一个控制器,但是有一对一的关系,这个控制器可以很好地与$expand一起使用相同的代码执行方法
你知道什么是失败的吗?
关于我发现这是OData Web Api库版本的问题 这就是问题所在:
这是否回答了您的问题?抱歉,这是正确的url。。。
// 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);