API 返回链接表 ID 的空值 [英] API returning null values for linked table IDs

查看:17
本文介绍了API 返回链接表 ID 的空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题一直困扰着我,非常感谢您的帮助.

This is a problem that I have been having a lot of trouble with and would be so grateful for some help.

我创建了一个链接到客户 ID 和手机 ID 的NewPurchases"API.但是,当我手动填充数据库中的采购"表并在 POSTMAN 上检查此 API 的 GET 功能时,它返回客户和手机 ID 的空值:

I have created a 'NewPurchases' API that is linked to a Customer ID and Handset ID. However, when I manually populate the 'Purchases' table in the database and check the GET functionality of this API on POSTMAN, it returns null values for Customer and 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 上发帖时,我收到 500 内部服务器错误无法创建类型为 'System.Collections.Generic.List...的空常量值......仅实体类型、枚举类型或原始类型在此上下文中支持类型."

Additionally, when I attempt to POST on POSTMAN, I get a 500 Internal Server Error "Unable to create a null constant value of type 'System.Collections.Generic.List....Only entity types, enumeration types or primitive types are supported in this context."

这是我的 NewPurchaseDto:

Here is my NewPurchaseDto:

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

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

And here is my NewPurchasesController (Api Controller):

 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();
    }
}
}

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

Additionally, here are my Customer and Handset models:

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; }
}

我真的很想深入了解这件事,提前致谢!

I really would love to get to the bottom of this, thanks in advance!

推荐答案

API 和参数看起来不错,我通过的时候可以进入操作:

The API and parameters look fine and I can get into the action when I pass:

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

所以它在 CreateNewPurchases 中失败了.我怀疑这条线

So it's failing inside CreateNewPurchases. I suspect the line

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

失败,因为您正在数据库中搜索手机,而这些手机位于 Id 的空 List<> 中,而 SQL 无法转换它.

is failing because you are searching for handsets in the database where they are in a null List<> of Ids, and SQL can't convert that.

将行改为

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

这篇关于API 返回链接表 ID 的空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆