SaveChangesAsync()挂在EF Core 3.1中

时间:2019-12-20 23:52:31

标签: asp.net-core entity-framework-core

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

我的SQL数据库中具有以下表结构:

VBA getting runtime error when trying sum function on named range

我允许用户“编辑”模板(更改操作/组)。保存后,我将删除模板的所有子项,然后重新添加新的子项(在DTO类中传递)。代码是:

   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;

它到达等待_context.SaveChangesAsync()的距离,并且似乎永远不会从该方法返回。 SQL Profiler没有显示活动。

任何人都可以看到导致问题的原因吗?

1 个答案:

答案 0 :(得分:0)

我设法使其正常运行,但不知道为什么我必须“两次”删除内容...

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

    template.TemplateCell
        .Clear();
    template.TemplateAction
        .Clear();
    template.TemplateGroup
        .Clear();
相关问题