繁体   English   中英

如何使用 Entity Framework Core 中的另一个对象更新子列表

[英]How to update child list with another object in Entity Framework Core

我需要从父级更新子列表向其添加记录或更新其属性之一。 我从控制器收到更新的模型,但是当我尝试用新模型替换实际列表并将更改保存到数据库时,我收到错误消息:

无法跟踪实体类型“WorkflowReferenciaExecucoes”的实例,因为已跟踪具有相同 {'ReferenciaExecucoesId'} 键值的另一个实例。 附加现有实体时,请确保仅附加一个具有给定键值的实体实例。 考虑使用 'DbContextOptionsBuilder.EnableSensitiveDataLogging' 来查看冲突的键值。

我无法直接访问dbContext ,因为我们使用的是存储库模式。 我试图在服务中更新孩子的是:

private void Update(Workflow entity)
{
    // entity is my updated model received by controller

    // Getting the actual parent in the database
    var workflow = GetById(entity.WorkflowId);
    workflow.NomeWorkflow = entity.NomeWorkflow;
    workflow.DescricaoWorkflow = entity.DescricaoWorkflow;
    workflow.FgAtivo = entity.FgAtivo;

    // Updating child list
    workflow.WorkflowReferenciaExecucoes = entity.WorkflowReferenciaExecucoes;

    // Trying to save the update gives error
    _uow.WorkflowRepository.Update(entity);
}

我的父类是:

public class Workflow
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int WorkflowId { get; set; }
    public int ProjetoId { get; set; }
    public int WorkflowTipoId { get; set; }
    public string NomeWorkflow { get; set; }
    public string DescricaoWorkflow { get; set; }
    public DateTime DataInclusao { get; set; }
    public bool FgAtivo { get; set; }
    public Projeto Projeto { get; set; }
    public WorkflowTipo WorkflowTipo { get; set; }
    public IEnumerable<WorkflowReferenciaExecucao> WorkflowReferenciaExecucoes { get; set; }
    public IEnumerable<WorkflowCondicaoExecucao> WorkflowCondicaoExecucoes { get; set; }
}

和子类:

public class WorkflowReferenciaExecucao
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int ReferenciaExecucaoId { get; set; }
    public int WorkflowId { get; set; }
    public int? ExecucaoWorkflowId { get; set; }
    public int ValorReferenciaExecucao { get; set; }
    public bool FgProcessar { get; set; }
    public bool FgAtivo { get; set; }
}

我该怎么做才能将实际列表更新为新列表?

谢谢!

是否可能是传入的entityWorkflowReferenciaExecucoes属性中有重复项 - 意味着相同的WorkflowReferenciaExecucao在该 IEnumerable 中存在两次?

你不能像那样更新你有错误的关系你的班级应该像那样

public class WorkflowReferenciaExecucao
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int ReferenciaExecucaoId { get; set; }
    public Workflow Workflow { get; set; }
    public int? ExecucaoWorkflowId { get; set; }
    public int ValorReferenciaExecucao { get; set; }
    public bool FgProcessar { get; set; }
    public bool Fugitive { get; set; }
}
WorkflowReferenciaExecucao is one and it has only one workflow so when you update Workflow then you have to update only workflow id in WorkflowReferenciaExecucao  don't pass whole object just pass id to change it one to many relationship so on one side you update anything it don't relate to many relationship because it only point to id that it

当更新实体中有多个具有相同ReferenciaExecucoesId子记录时,我可以重现您的问题。

您可以检查是否是这种情况。

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM