ToListAsync Enumeration yielded no results c# entity-framework-core linq


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

    public VoucherCampaigns VoucherCampaigns { get; set; }
    public CouponTypes CouponTypes { get; set; }
    public CouponBrand CouponBrand { get; set; }


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


            entity.Property(e => e.Id)

            entity.Property(e => e.CampaignId)

            entity.Property(e => e.CertificateId)

The database table name in SQL is dbo.CertificateMaster.

Any help is greatly appreciated. Thanks!

EDIT: Debug gift.ToListAsync()

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)

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

Related Questions


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