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

本文是小编为大家收集整理的关于返回链接表ID的空值的API的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

这是一个问题,我一直在遇到很多麻烦,并会非常感激一些帮助.

我创建了一个与客户ID和手机ID相关联的'newpurchases'api.但是,当我手动填充数据库中的"购买"表并在邮递员上检查此API的获取功能时,它会返回客户和手机ID的空值:

<Purchase>
 <CommissionGenerated>100</CommissionGenerated>
 <Customer i:nil="true"/>
 <DatePurchased>2018-02-15T00:00:00</DatePurchased>
 <Handset i:nil="true"/>
 <Id>3</Id>
</Purchase>
此外,当我尝试在邮递员上发布时,我得到了500个内部服务器错误"无法创建类型"system.gollections.generic.list ....只有实体类型,枚举类型或原始在此上下文中支持类型."

这是我的新网站:

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

在这里是我的新型Curchasescontroller(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; }
}

我真的很乐意进入这个底部,谢谢你提前谢谢!

推荐答案

api和参数看起来很好,当我通过时,我可以进入动作:

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

所以它在CreateNewPurchases内部失败.我怀疑

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

失败,因为您正在搜索数据库中的手机,其中它们位于ID的NULL List<>中,并且SQL无法转换.

将行更改为

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

本文地址:https://www.itbaoku.cn/post/1937860.html

问题描述

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!

推荐答案

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