【发布时间】:2018-03-08 14:25:09
【问题描述】:
我重新编辑了这个问题。
我有一个 API 方法应该返回一个客户,包括来自其他表的数据。当我使用 Swagger 测试 API 时,我得到了一些 ID 的值,但没有获得 customerId == 1 的值。
DAL 客户模型类:
public class Customer : AuditableEntity
{
public int Id { get; set; }
public string Name { get; set; }
// other props deleted for readability
public ICollection<Shipping> Shippings {get; set; }
public ICollection<Billing> Billings {get; set; }
public ICollection<Payer> Payers {get; set; }
public ICollection<Logistic> Logistics {get; set; }
}
运输模型类
public class Shipping : AuditableEntity
{
public int Id { get; set; }
public string Name { get; set; }
// other props deleted for readability
public int CustomerId { get; set; }
public Customer Customer { get; set; }
}
客户视图模型
public class CustomerViewModel
{
public int Id { get; set; }
public string Name { get; set; }
// other props deleted for readability
public ICollection<ShippingViewModel> Shippings { get; set; }
public ICollection<BillingViewModel> Billings { get; set; }
public ICollection<PayerViewModel> Payers { get; set; }
public ICollection<LogisticViewModel> Logistics { get; set; }
}
public class CustomerViewModelValidator : AbstractValidator<CustomerViewModel>
{
public CustomerViewModelValidator()
{
RuleFor(register => register.Name).NotEmpty().WithMessage("Customer name cannot be empty");
}
}
存储库:
public class CustomerRepository : Repository<Customer>, ICustomerRepository
{
public CustomerRepository(ApplicationDbContext context) : base(context)
{ }
public Customer GetCustomerById(int customerId)
{
return _appContext.Customers
.Include(c => c.Shippings)
.Include(c => c.Billings)
.Include(c => c.Payers)
.Include(c => c.Logistics)
.Single(x => x.Id == customerId)
//.SingleOrDefault(x => x.Id == customerId)
;
}
API 控制器
[Authorize(AuthenticationSchemes = IdentityServerAuthenticationDefaults.AuthenticationScheme)]
[Route("api/[controller]")]
public class CustomerController : Controller
{
private readonly IAuthorizationService _authorizationService;
private IUnitOfWork _unitOfWork;
readonly ILogger _logger;
readonly IEmailer _emailer;
public CustomerController(IUnitOfWork unitOfWork, ILogger<CustomerController> logger, IEmailer emailer,
IAuthorizationService authorizationService)
{
_unitOfWork = unitOfWork;
_logger = logger;
_emailer = emailer;
_authorizationService = authorizationService;
}
// GET api/value/5
[HttpGet("{id}")]
public IActionResult GetCustomerById(int id)
{
var customer = _unitOfWork.Customers.GetCustomerById(id);
return Ok(Mapper.Map<Customer>(customer));
}
数据库中表 customer 的值是 1 到 6。 表 shipping 包含与 customerId == 1 相关联的 1 个值。
当我测试 API 时,我无法获取 customerId == 1 的值。它导致响应代码 0。Header == "error": "no response from server"。并且响应正文 =“无内容”。
当我用 ID == 2, 3, ... 测试它时,我可以获得以下值:
响应码 == 200
{
“id”:2,
"名称": "Inion",
// 为了便于阅读,移除了其余部分
“运费”:[],
“帐单”:[],
“付款人”:[],
"物流": [],
"updatedDate": "2018-02-08T15:41:45.4929139",
"createdDate": "2018-02-08T15:41:45.4929139"
}
【问题讨论】:
-
我没有看到任何
OrderBy,所以这个问题没有任何意义,因为数据库表没有内在的顺序。 -
Single如果有两个结果会抛出异常,所以你的问题没有意义 -
@TimSchmelter:当 OP 说第一个没有返回但其他的不是在单个结果集中讨论时。它们的意思是,当查询 id=1 时,不会返回任何文档,但是当查询 id=2 等等时,文档会按预期返回。因此,OP 询问为什么当 id=1 时没有返回任何内容,而当 id>1 时却没有返回。每个查询应该只返回一个结果,但据我了解,因此排序无关紧要。
-
如果
Includes为 id 2 工作,但不是 1,我会看看它们之间有什么区别。这些导航属性的外键值是什么?还要分析生成的 SQL。最后Single应该返回一个值或抛出,所以你有没有抓住和异常?也许显示处理请求并调用您的方法的代码? -
您正在显示您的数据访问代码,而问题在于调用它的代码。显示您的 API 控制器。
标签: c# entity-framework linq controller .net-core