SaveChangesAsync() is hanging in EF Core 3.1

asp.net-core entity-framework-core

Question

VS 2019 ASP.NET Core 3.1 EF Core 3.1 Web API

I have the following table structure in my SQL database:

enter image description here

I allow users to "edit" the template (change actions/groups). When they save, I delete all of the children of the template and re-add the new children (passed in a DTO class). The code is:

   var now = DateTime.UtcNow;

    var template = await _context.Template
        .Include(t => t.TemplateCell)
        .Include(t => t.TemplateGroup)
        .ThenInclude(t => t.TemplateCell)
        .Include(t => t.TemplateAction)
        .ThenInclude(t => t.TemplateCell)
        .FirstOrDefaultAsync(t => t.Id == dto.Id);

    // Updates the templates details
    template.Name = dto.Name;
    template.Description = dto.Description;
    template.InitialRisk = dto.InitialRisk;
    template.ModifiedWhen = now;
    template.ModifiedByUserId = user.Id;

    // Clear the old children
    template.TemplateCell
        .Clear();
    template.TemplateAction
        .Clear();
    template.TemplateGroup
        .Clear();

    // Add the new children
    int sortOrder = 0;
    foreach (var groupName in dto.GroupNames)
    {
        template.TemplateGroup
            .Add(new TemplateGroup
            {
                Name = groupName,
                SortOrder = sortOrder++
            });
    }

    sortOrder = 0;
    foreach (var actionName in dto.ActionNames)
    {
        template.TemplateAction
            .Add(new TemplateAction
            {
                Name = actionName,
                SortOrder = sortOrder++
            });
    }

    int cellIndex = 0;
    foreach (var group in template.TemplateGroup)
        foreach (var action in template.TemplateAction)
            template.TemplateCell
                 .Add(new TemplateCell
                 {
                     TemplateGroup = group,
                     TemplateAction = action,
                     IsEnabled = dto.Cells[cellIndex++]
                 });

    await _context.SaveChangesAsync();  // Does not seem to ever return from here?

    return template.Id;

It get's as far as the await _context.SaveChangesAsync() and never seems to return from that method. SQL Profiler shows no activity.

Can anyone see what may be causing the issue?

1
0
12/20/2019 11:58:18 PM

Popular Answer

I managed to get it working, but have no idea why I had to delete things "twice"...

    _context.TemplateCell.RemoveRange(template.TemplateCell);
    _context.TemplateAction.RemoveRange(template.TemplateAction);
    _context.TemplateGroup.RemoveRange(template.TemplateGroup);

    template.TemplateCell
        .Clear();
    template.TemplateAction
        .Clear();
    template.TemplateGroup
        .Clear();
0
12/21/2019 12:46:23 AM


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