API为链接表ID返回空值

时间:2022-08-19 09:51:50

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

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

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:

我创建了一个链接到客户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>

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."

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

Here is my NewPurchaseDto:

这是我的NewPurchaseDto:

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

And here is my NewPurchasesController (Api Controller):

这是我的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();
    }
}
}

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!

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

1 个解决方案

#1


0  

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

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

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

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

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

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

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.

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

Change the line to

将行更改为

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

#1


0  

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

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

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

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

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

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

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.

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

Change the line to

将行更改为

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