API为链接表ID返回空值

时间:2018-02-17 18:33:42

标签: asp.net-mvc api-design

这是一个我遇到很多麻烦的问题,非常感谢你的帮助。

我创建了一个链接到客户ID和手机ID的'NewPurchases'API。但是,当我手动填充数据库中的“Purchases”表并在POSTMAN上检查此API的GET功能时,它会返回Customer和Handset ID的空值:

<Purchase>
 <CommissionGenerated>100</CommissionGenerated>
 <Customer i:nil="true"/>
 <DatePurchased>2018-02-15T00:00:00</DatePurchased>
 <Handset i:nil="true"/>
 <Id>3</Id>
</Purchase>

此外,当我尝试在POSTMAN上POST时,我得到500内部服务器错误“无法创建类型为'System.Collections.Generic.List的空常量值....仅实体类型,枚举类型或基元在这种情况下支持类型。“

这是我的NewPurchaseDto:

public int CustomerId { get; set; }
    public List<int> HandsetIds { get; set; }

这是我的NewPurchasesController(Api控制器):

 public class NewPurchasesController : ApiController
{
    private ApplicationDbContext _context;

    public NewPurchasesController()
    {
        _context = new ApplicationDbContext();
    }

    // Get Api/NewPurchases
    public IHttpActionResult GetHandsets(NewPurchaseDto newPurchase)
    {
        var handsetsQuery = _context.Purchases.ToList();

        return Ok(handsetsQuery);

    }

    [HttpPost]
    public IHttpActionResult CreateNewPurchases(NewPurchaseDto newPurchase)
    {

        var customer = _context.Customers.Single(
        c => c.Id == newPurchase.CustomerId);

        var handsets = _context.Handsets.Where(
        m => newPurchase.HandsetIds.Contains(m.Id)).ToList();

        foreach (var handset in handsets)
        {
            var purchase = new Purchase
            {
                Customer = customer,
                Handset = handset,
                DatePurchased = DateTime.Now
            };

            _context.Purchases.Add(purchase);
        }
        _context.SaveChanges();

        return Ok();
    }
}
}

此外,这是我的客户和手机型号:

public class Customer
{
    public int Id { get; set;}

    [Required]
    [StringLength(255)]
    public string Name { get; set; }

    public bool IsSubscribedToInsurance { get; set; }

    [Display(Name = "Account Type")]
    public AccountType AccountType { get; set; }

    public byte AccountTypeId { get; set; }

    [Display(Name = "Date of Birth")]
    [Min18YearsIfAContract]
    public DateTime? Birthdate { get; set; }
}



public class Handset
{
    public int Id { get; set; }

    [Required]
    [StringLength(255)]
    public string Name { get; set; }



    public Manufacturer Manufacturer { get; set; }
    [Display(Name = "Manufacturer")]
    [Required]
    public byte ManufacturerId { get; set; }
    public DateTime DateAdded { get; set; }

    [Display(Name = "Release Date")]
    public DateTime ReleaseDate { get; set; }

    [Display(Name = "Number in Stock")]
    [Range(1, 25)]
    public byte NumberInStock { get; set; }
}

我真的很想深究这一点,提前谢谢!

1 个答案:

答案 0 :(得分:0)

API和参数看起来很好,我可以在通过时进入操作:

{
    customerId: 123,
    handsetIds: [1,2,3]
}

所以它在CreateNewPurchases内失败了。我怀疑这条线

var handsets = _context.Handsets.Where(
    m => newPurchase.HandsetIds.Contains(m.Id)).ToList();

失败是因为您正在数据库中搜索它们位于Ids的空List<>内的手机,并且SQL无法转换它。

将行更改为

var handsets = _context.Handsets.Where(
    m => newPurchase.HandsetIds.Any(np => np == m.Id));