我重新编辑了这个问题。
我有一个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; }
}
CustomerViewModel
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));
}
数据库的表客户值为1到6。 表格发送包含1个链接到customerId == 1的值。
当我测试API时,我无法获取customerId == 1的值。它会产生响应代码0.标题==&#34;错误&#34;:&#34;服务器没有响应&#34; 。响应主体=&#34;没有内容&#34;。
当我用ID == 2,3,...测试时,我可以获得值ex:
响应代码== 200
{
&#34; id&#34;:2,
&#34; name&#34;:&#34; Inion&#34;,
//为了便于阅读而删除休息
&#34; shippings&#34;:[],
&#34;账单&#34;:[],
&#34;付款人&#34;:[],
&#34;后勤&#34;:[],
&#34; updatedDate&#34;:&#34; 2018-02-08T15:41:45.4929139&#34;,
&#34; createdDate&#34;:&#34; 2018-02-08T15:41:45.4929139&#34;
}
答案 0 :(得分:0)
感谢所有快速反应的人,如果我不清楚,请对不起。
检查完db后,我找不到错误。
@CodeCaster指向API控制器,忽略了我映射错误的类。
当然我有其他ID为空标签的返回值,因为其他ID没有在其他表中链接的值,只有id = 1。我已经对更改进行了测试,并在链接表中添加了更多数据,以确保所有数据都按预期工作。
正确的API控制器方法
[HttpGet("{id}")]
public IActionResult GetCustomerById(int id)
{
var customer = _unitOfWork.Customers.GetCustomerById(id);
return Ok(Mapper.Map<CustomerViewModel>(customer));
}