API returning null values for linked table IDs

你离开我真会死。 提交于 2019-12-11 04:27:58

问题


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:

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

Here is my NewPurchaseDto:

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

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!


回答1:


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

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

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

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.

Change the line to

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


来源:https://stackoverflow.com/questions/48844603/api-returning-null-values-for-linked-table-ids

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!