我正在尝试获取包含具有特定ID的网站的所有类别的列表。下面是我稍微修改过的脚手架生成方法。
[HttpGet("Categories/{id}")]
public async Task<IActionResult> GetCategoriesSite([FromRoute] Guid id)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
// This line throws the error
var categories = await _context.Categories.Where(o => o.Site.Id.Equals(id)).ToListAsync();
if (categories == null)
{
return NotFound();
}
return Ok(categories);
}
不幸的是,当我运行它时会抛出以下错误:
System.ArgumentException: Expression of type 'System.Nullable`1[System.Guid]' cannot be used for parameter of type 'System.Guid' of method 'Boolean Equals(System.Guid)'
实体非常简单:
public class Category
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Site Site { get; set; }
}
public class Site
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public ICollection<Category> Categories { get; set; }
}
我可能做错了什么?
答案 0 :(得分:3)
看起来像EF Core中的一个错误,请在github上查看issue。将Equals()
替换为==
运算符的解决方法对我来说很好:
var categories = await _context.Categories.Where(o => o.Site.Id == id).ToListAsync();