【问题标题】:Unable to do multi-table item create in Entity-Framework Core无法在 Entity-Framework Core 中创建多表项
【发布时间】:2019-12-19 03:27:08
【问题描述】:

我有一个可用的 ASP.NET Web API,我正在尝试将其转换为 ASP.NET Core。我正在努力添加多表项目。我有以下 4 个表(SQL Server):

模板, TemplateAction - 带有模板的外键, TemplateGroup - 具有模板的外键, TemplateCell - 具有 Template 的外键、TemplateAction 的外键和 TemplateGroup 的外键。

添加“模板”(一个TemplateAction,一个TemplateGroup,一个TemplateCell)的代码如下:

  public async Task<int> CreateTemplate(string userId, TemplateCreateDTO dto)
{
    using (var context = MyDataContext.Instance) // Injected in ASP.NET Core (no using)
    {
        var now = DateTime.UtcNow;

        // Create s single default Group
        var groups = new[]
        {
            new TemplateGroup
            {
                Name = "Row Name",
                Description = string.Empty,
                SortOrder = 0
            }
        };

        // Create s single default Action
        var actions = new[]
        {
            new TemplateAction
            {
                Name = "Column Name",
                Description = string.Empty,
                SortOrder = 0
            }
        };

        // All cells are enabled when a Template is created
        var cells = new[]
        {
            new TemplateCell
            {
                TemplateGroupId = groups[0].Id,
                TemplateActionId = actions[0].Id,
                IsEnabled = true
            }
        };

        var template = new Template
        {
            Name = dto.Name,
            Description = dto.Description,
            InitialRisk = dto.InitialRisk,
            CreatedWhen = now,
            CreatedByUserId = userId,
            ModifiedWhen = now,
            ModifiedByUserId = userId,
            TemplateGroups = groups,
            TemplateActions = actions,
            TemplateCells = cells
        };

        context.Templates
            .Add(template);

        await context.SaveChangesAsync();

        return template.Id;
    }
}

ASP.NET Core 3.1 (EF Core 3.1) 中的相同代码 - 除了上下文被注入 - 失败并出现以下错误:

The INSERT statement conflicted with the FOREIGN KEY constraint \"FK_TemplateCell_TemplateAction\". The conflict occurred in database \"MyDB\", table \"dbo.TemplateAction\", column 'Id'.\r\nThe statement has been terminated.

我尝试了很多东西,但无法添加模板。谁能看到问题可能是什么?谢谢。

【问题讨论】:

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


    【解决方案1】:
    var cells = new[]
            {
                new TemplateCell
                {
                    TemplateGroupId = groups[0].Id,
                    TemplateActionId = actions[0].Id,
                    IsEnabled = true
                }
            };
    

    当您尝试保存时,id 将没有任何价值..

    我猜你必须做这样的事情

    var cells = new[]
            {
                new TemplateCell
                {
                    TemplateGroup = groups[0],
                    TemplateAction = actions[0],
                    IsEnabled = true
                }
            };
    

    当引用的对象被保存时,它会为你设置 fk..

    无论如何,如果您真的希望所有模板都有一个 fk,您应该使用复合主键来确保您的组/操作/单元格指向同一个模板,对吗?

    或者期望他们可以引用不同的模板?

    【讨论】:

    • 单个不相交事物中的模板/操作/组/单元格(无跨模板引用)。所以没有多对多。数据库与迁移到核心之前一样。
    • 修复了它。标记为答案。我看不到树的第一个。谢谢。
    猜你喜欢
    • 2017-10-28
    • 2022-12-10
    • 1970-01-01
    • 2018-09-10
    • 1970-01-01
    • 2020-01-18
    • 1970-01-01
    • 2015-01-15
    • 1970-01-01
    相关资源
    最近更新 更多