1

Have been on this for hours now and even after searching other solutions online, I'm still not able to understand/resolve this error!

I have a blazor application where I'd like to load data, via a DI service, when the index page is initialized. Below is a summarized version of my code:

Startup.cs

...
services.AddDbContext<MyDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DbContext")));
...
services.AddTransient<IMyDbService, MyDbService>();
...

DbContext.cs

public class MyDbContext : DbContext
    {
        public MyDbContext() { }

        public MyDbContext(DbContextOptions<MyDbContext> options) : base(options) { }

        public virtual DbSet<Item> Items { get; set; }
    }

IMyDbService.cs

public class MyDbService : IMyDbService
    {
        private MyDbContext MyDbContext { get; set; }

        public MyDbService (MyDbContext myDbContext)
        {
            MyDbContext = myDbContext;
        }

public async Task<IEnumerable<Item>> Get()
        {
            var result = await MyDbContext.Items
                .Include(i => i.Log)
                .ToListAsync();
            return result;
        }
    }

and finally calling within

Index.razor.cs

public partial class Index
    {
        [Inject]
        protected IMyDbService MyDbService { get; set; }
        public Dashboard Dashboard { get; set; } = new Dashboard();
        protected override async Task OnInitializedAsync()
        {
            await base.OnInitializedAsync();
            Dashboard.Items = await MyDbService.Get();
        }
    }

I've walked through the code and the exception is thrown on the LINQ query

var result = await MyDbContext.Items
                .Include(i => i.Log)
                .ToListAsync();

screenshot of error

I've tried:

  • reading blazor documentation
  • removing the .Include(...) - same error
  • added a debug code that does a synchronous call within the same method - var t = MyDbContext.Items.Include(i => i.Log).ToList();. This works
  • going to sleep thinking on this problem, and waking up the next day - same error.

I must be overlooking something ?

EDIT With thanks to Henk Holterman, realized that the exception stack message shows a .Where() being called... which, by following the code, is being called within the index component view. Have also updated index.razor.cs with more accurate code.

<div class="row text-center">
                <div class="col">
                    ...
                    <h4>@Dashboard.Items.Where(x => x.Status == ItemStatus.New).Count()</h4>
                    ...
                </div>
            </div>
     </div>
Luv2Learn
  • 130
  • 1
  • 1
  • 9
  • Change private MyDbContext MyDbContext { get; set; } to private readonly MyDbContext MyDbContext and let the injector set it – GH DevOps Aug 04 '21 at 17:00
  • @GHDevOps changing `private MyDbContext MyDbContext { get; set; }` to `private readonly MyDbContext MyDbContext;` gives the same error. – Luv2Learn Aug 04 '21 at 17:12
  • The stacktrace says the error is in a `.Where()` . There is no `.Where()` in the posted code. Ergo, you posted the wrong code. – H H Aug 04 '21 at 17:56
  • @HenkHolterman Thought the same but got confused since stepping over the breakpoint returned with an exception immediately. But you prompted me to strip some components using .Where() on the index page, the error went away! Thanks. Will update this post with the answer and additional code from my index page. – Luv2Learn Aug 04 '21 at 18:34
  • What does the class Items look like? Are you initializing an instance of Log inside that constructor or somewhere else in code? – GH DevOps Aug 04 '21 at 18:47

1 Answers1

2

Ahh found a solution! The exception is being triggered by the following section within index.razor. The first time the index component loads, it seems the Dashboard.Items is still null.

So a fix is to replace

<h4>@Dashboard.Items.Where(x => x.Status == ItemStatus.New).Count()</h4>

with

<h4>@Dashboard.Items?.Where(x => x.Status == ItemStatus.New).Count()</h4>
Luv2Learn
  • 130
  • 1
  • 1
  • 9