Another look at using .Includes when using Entity Framework to query a SQL Server database as part of a Blazor Server app..

In getting content for an entity from a database table, this is typically a call such as the following ,to get all Activities in the Activity table.

    public async Task<List<Activity>> GetActivitys()
    {
        var list = await _context.Activitys.ToListAsync();
        return list;
    }

As discussed in previous posts, the Activity class has a number of scalar properties, such as the Task which is a string, but it also has some properties that are entities themselves. These compound properties are Helper and Round:

Helpers volunteer for Activities. An Activity is for a specific Round with a specific Helper in a calendar of Track Meets.

When rendering Activity data, the other entities need to be tracked by the DbContext. This is handled by adding some .Include appendages to the selection query such as:

    public async Task<List<Activity>> GetActivitys()
    {
        var list = await _context.Activitys
            .Include(activity => activity.Helper)
            .Include(activity => activity.Round).ToListAsync();
        return list;
    }

I am repeating myself a little here but I am doing so because I ran into a related problem with the Helpers app last night. The app is configured to send an SMS message to all volunteers requesting a confirmation Y or N response via SMS which is interpreted by the app and forwarded to an Admin via SMS. The error was that the app reads in settings from the database such as who from the Helpers is the designated Admin to receive the processed replies. The app users that Helper’s mobile number to forward the replies to. Unfortunately the app was reading the Admin helper as null and so the reception and on-forwarding of replies fell over when most volunteers quickly replied to the confirmation requests.

Whilst working to resolve the error in the code, I was able to recover the situation manually by going through the SMS logs and reconcile the responses with the volunteers. Rather tedious though.

The relevant code is:

    var list = await _context.TwSettings.ToListAsync();
    if (list.Count() == 0)
    {
        await SaveSettingsFromAppSettings();
        list = await _context.TwSettings.ToListAsync();
    }
    setting = list.LastOrDefault();

Explanation: This code reads in the list of entries in the TwSettings table and takes the last entry as the settings. If there are no entries, it saves a static set of settings in a static class in the code. The errant code read a null value for the .EmPerson property which is a Helper entity.
The code was modified with a .Include:

    var list = await _context.TwSettings.Include(twsetting => twsetting.EmPerson).ToListAsync();
    if (list.Count() == 0)
    {
        await SaveSettingsFromAppSettings();
        list = await _context.TwSettings.Include(twsetting => twsetting.EmPerson).ToListAsync();
    }
    setting = list.LastOrDefault();

That worked, so that later responses did go through seamlessly.

But then due to COVID-19, we went in to local lockdown the day of the event for 5 days so the event got cancelled! Hopefully we will be out of lockdown for the next Round and so the app will be of use then.


Next: SMS functionality using Twilio.


 TopicSubtopic
  Next: > Blazor Helpers App Twilio
<  Prev:   Azure Devops Jekyll Site
   
 This Category Links 
Category:Blazor Index:Blazor
  Next: > Blazor Helpers App
<  Prev:   Blazor Helpers App