LinkedListT
LinkedListT

Reputation: 691

Why does using ToListAsync yield no results?

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

Upvotes: 2

Views: 1596

Answers (1)

Nkosi
Nkosi

Reputation: 247068

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

Upvotes: 1

Related Questions