簡體   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