Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# API未返回所有预期值_C#_Entity Framework_Linq_Controller_.net Core - Fatal编程技术网

C# API未返回所有预期值

C# API未返回所有预期值,c#,entity-framework,linq,controller,.net-core,C#,Entity Framework,Linq,Controller,.net Core,我重新编辑了这个问题 我有一个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 readabi

我重新编辑了这个问题

我有一个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; }

}
CustomerServiceWModel

 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类CustomerViewModel
{
公共int Id{get;set;}
公共字符串名称{get;set;}
//为便于阅读,删除了其他道具
公共ICollection Shippings{get;set;}
公共ICollection Billings{get;set;}
公共ICollection付款人{get;set;}
公共ICollection物流{get;set;}
}
公共类CustomerViewModelValidator:抽象验证器
{
公共CustomerViewModelValidator()
{
RuleFor(register=>register.Name).NotEmpty().WithMessage(“客户名称不能为空”);
}
}
存储库:

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)               
            ;
    }
公共类CustomerRepository:存储库,ICCustomerRepository
{
公共CustomerRepository(ApplicationDbContext上下文):基础(上下文)
{ }
公共客户GetCustomerById(int customerId)
{
return\u appContext.Customers
.包括(c=>c.装运)
.包括(c=>c.Billings)
.包括(c=>c.付款人)
.包括(c=>c.物流)
.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));
    }
[授权(AuthenticationSchemes=IdentityServerAuthenticationDefaults.AuthenticationScheme)]
[路由(“api/[控制器]”)]
公共类CustomerController:控制器
{
私有只读IAuthorizationService\u授权服务;
私人工作单位;
只读ILogger\u记录器;
只读电子邮箱;
公共客户控制器(iUnitof Work、ILogger logger、iEmailr emailer、,
IAAuthorizationService(授权服务)
{
_unitOfWork=unitOfWork;
_记录器=记录器;
_emailer=emailer;
_授权服务=授权服务;
}
//获取api/value/5
[HttpGet(“{id}”)]
公共IActionResult GetCustomerById(内部id)
{
var customer=\u unitOfWork.Customers.GetCustomerById(id);
返回Ok(Mapper.Map(customer));
}
对于表customer,数据库的值为1到6。 表shippings包含一个链接到customerId==1的值

当我测试API时,我无法获取customerId==1的值。这会导致响应代码0。Header==“error”:“no response from server.”和响应正文=“no content”

当我用ID==2,3,…测试它时,我可以得到值ex:
响应代码==200
{
“id”:2,
“名称”:“Inion”,
//为便于阅读,其余部分已删除
“发货”:[],小于br> “账单”:[],
“付款人”:[],
“物流”:[],
“更新日期”:“2018-02-08T15:41:45.4929139”,
“createdDate”:“2018-02-08T15:41:45.4929139”

}感谢所有快速响应的人,如果我不清楚,请原谅

检查完数据库后,我没有发现什么问题

@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));
    }
[HttpGet(“{id}”)]
公共IActionResult GetCustomerById(内部id)
{
var customer=\u unitOfWork.Customers.GetCustomerById(id);
返回Ok(Mapper.Map(customer));
}

我看不到任何
OrderBy
,因此这个问题没有任何意义,因为数据库表没有固有的顺序。
Single
如果有两个结果,则抛出异常,因此您的问题没有意义sense@TimSchmelter:当OP表示第一个未返回,但其他未返回时,他们不会在单个结果中谈论ts集。这意味着当查询id=1时,不会返回任何文档,但当查询id=2等时,文档会按预期返回。因此OP询问为什么当id=1时不会返回任何文档,而当id>1时会返回任何文档。每个查询应该只返回一个结果,尽管据我所知,排序是不相关的。如果de>包含id 2,但不是1。我想看看它们之间有什么区别。这些导航属性的外键值是什么?还要分析生成的SQL。最后,
Single
应该返回值或抛出,所以您是否有机会捕获和异常?可能会显示处理的代码这是您的方法的请求和调用吗?您正在显示数据访问代码,而调用此方法的代码存在问题。显示您的API控制器。
    [HttpGet("{id}")]
    public IActionResult GetCustomerById(int id)
    {
        var customer = _unitOfWork.Customers.GetCustomerById(id);
        return Ok(Mapper.Map<CustomerViewModel>(customer));
    }