ToListAsync Enumeration yielded no results

asp.net-core-mvc c# entity-framework-core linq

Question

I am having a hard time with this so I hope you can help.

In my GiftCertificateController.cs (I have used a LINQ query in the index method)

private readonly VoucherDbContext _context;

    public GiftCertificateController(VoucherDbContext context)
    {
        _context = context;
    }

    public async Task<IActionResult> Index()
    {
        var gifts = _context.GiftCertificates
            .Include(e => e.VoucherCampaigns)
            .Include(e => e.CouponBrand)
            .Include(e => e.CouponTypes);

        return View(await gifts.ToListAsync());
    }

Now during debugging however, hovering over 'gifts.ToListAsync' shows ' Ienumerable: enumeration yielded no results'.

What's odd is I use the exact same pattern (same business entity structure, same entity mapping structure, same view structure) for 3 different controllers and they all seem to be working fine, but for this controller in particular, it is not able to show a single database properties. (The table has 30+ rows)

This is an excerpt of the business entity GiftCertificates.cs:

public partial class GiftCertificates
{
    [Key]
    public int Id { get; set; }
    public string CertificateId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public decimal? CertificateAmount { get; set; }
    public decimal? CertificateValue { get; set; }
    public int? TypeId { get; set; }
    public int? CategoryId { get; set; }
    public int? BrandId { get; set; }

    [ForeignKey("CampaignId")]
    public VoucherCampaigns VoucherCampaigns { get; set; }
    [ForeignKey("TypeId")]
    public CouponTypes CouponTypes { get; set; }
    [ForeignKey("BrandId")]
    public CouponBrand CouponBrand { get; set; }

}

And this is an excerpt of the entity mapping in my DbContext:

entity.ToTable("CertificateMaster");

            entity.Property(e => e.Id)
            .IsRequired()
            .HasColumnName("Id");

            entity.Property(e => e.CampaignId)
            .HasColumnName("CampaignId");

            entity.Property(e => e.CertificateId)
            .HasColumnName("CertificateId");

The database table name in SQL is dbo.CertificateMaster.

Any help is greatly appreciated. Thanks!

EDIT: Debug gift.ToListAsync()

1
1
11/5/2018 1:18:43 PM

Accepted Answer

Ideally you want to await the result first and then pass that to the view

public async Task<IActionResult> Index() {
    var gifts = await _context.GiftCertificates
        .Include(e => e.VoucherCampaigns)
        .Include(e => e.CouponBrand)
        .Include(e => e.CouponTypes)
        .ToListAsync();

    return View(gifts);
}
1
7/11/2018 10:05:50 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow