SaveChangesAsync()挂在EF Core 3.1中

问题描述 投票:0回答:1

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

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

enter image description here

我允许用户“编辑”模板(更改操作/组)。保存后,我将删除模板的所有子项,然后重新添加新的子项(在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事件探查器未显示任何活动。

谁能看到导致问题的原因吗?

asp.net-core entity-framework-core
1个回答
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();
© www.soinside.com 2019 - 2024. All rights reserved.