简体   繁体   English

如何通过 PUT 方法更新 ICollection 的属性?

[英]How to update a property of an ICollection through PUT method?

I have two classes, Ignicoes and Ocorrencias: Ignicoes class:我有两个班级,Ignicoes 和 Ocorrencias: Ignicoes 班级:

 public class Ignicoes
{
    public enum EstadoIgnicao
    {
        aceite,
        emAvaliacao,
        concluido,
        recusado
    }

    public Ignicoes()
    {
        ListaOcorrencias = new HashSet<Ocorrencias>();

    }

    
    [Key]
    [Required]
    public int Id { get; set; }

    [Required]
    public string Latitude { get; set; }

    [Required]
    public string Longitude { get; set; }

    //estado(recusada, aceite, em avaliacao, concluido)
  
    [Required]
    public EstadoIgnicao Estado { get; set; }

  
    public DateTime DataInicioPropostaIgnicao { get; set; }
    public DateTime DataDecisaoIgnicao { get; set; }


    //lista de ocorrencias 
    public virtual ICollection<Ocorrencias> ListaOcorrencias { get; set; }

}

Ocorrencias class:奥科伦西亚斯类:

      public class Ocorrencias
{

    public enum EstadoOcorrencia
    {
        aceite,
        emAvaliacao,
        recusado
    }

    [Key]
    public int Id { get; set; }

    /// <summary>
    /// código que identifica de forma única o aparelho que comunica a ocorrência
    /// </summary>
    [Required]
    public string Dispositivo { get; set; }


    /// <summary>
    /// data da ocorrencia
    /// </summary>
    [Required]
    public DateTime DataOcorrencia { get; set; }

    /// <summary>
    /// coordenadas GPS - Latitude
    /// </summary>
    [Required]
    public string Latitude { get; set; }

    /// <summary>
    /// coordenadas GPS - Logitude
    /// </summary>
    [Required]
    public string Longitude { get; set; }

    /// <summary>
    /// Azimute do ?angulo formado entre o Polo Norte e o fogo
    /// </summary>
    [Required]
    public string Azimute { get; set; }


    /// <summary>
    /// Foto a provar a ocorrência
    /// </summary>
    [Required]
    public string Fotografia { get; set; }

    /// <summary>
    /// Nome a atribuir à fotografia guardada no disco rígido
    /// </summary>
    public string NomeFotografia { get; set; }

    /// <summary>
    /// estado da ocorrencia : aceite, em avaliação, recusado
    /// </summary>
    [Required]
    public EstadoOcorrencia Estado { get; set; }

    [ForeignKey("Ignicao")]
    [Display(Name = "Ignicao")]
    public int? IgnicaoFK { get; set; }
    public virtual Ignicoes Ignicao { get; set; }



}

As you can see every Ignicao has a list of Ocorrencias.正如您所看到的,每个 Ignicao 都有一个 Ocorrencias 列表。 I update the property "Estado" of the Ignicao through an ajax request that calls the PUT method我通过调用 PUT 方法的 ajax 请求更新了 Ignicao 的属性“Estado”

function atualizaBD(idmarcador, novoEstado, latitude, longitude) {
    $.ajax
        ({
            url: `/api/IgnicoesAPI/${idmarcador}`,
            type: 'PUT',
            contentType: "application/json; charset=utf-8",
            dataType: 'json',
            data: JSON.stringify({
                Id: idmarcador,
                Estado: novoEstado,
                Latitude: latitude,
                Longitude: longitude
            }),
            async: true,
            processData: false,
            cache: false,
            success: function (result) {
                 connection.invoke("PostMarker").catch(function (err) {
                              return console.error(err.toString());
                 });
            },
            error: function () {
                alert(novoEstado)
            }
        });
}

Here is my PUT method:这是我的 PUT 方法:

   public async Task<IActionResult> PutIgnicoes([FromRoute] int id, [FromBody] Ignicoes ignicao)
    {

        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }


        if (id != ignicao.Id)
        {
            return BadRequest();
        }

        else
        {
            var dataDecisao = DateTime.Now;
            var ig = _context.Ignicoes.FirstOrDefault(ignicaoId => ignicaoId.Id.Equals(id));
            if (ig != null)
            {



                ig.Estado = ignicao.Estado;

                //é necessário mudar o estado das ocorrencias que fazem parte da lista de ocorrencias desta ignição
                var listaOocrrencias = ig.ListaOcorrencias.ToList();
                for(int i=0; i < listaOocrrencias.Count;i++)
                {
                    if (ignicao.Estado == Ignicoes.EstadoIgnicao.aceite)
                    {
                        ig.ListaOcorrencias.ElementAt(i).Estado = Ocorrencias.EstadoOcorrencia.aceite;
                    }
                    else
                    {
                        if (ignicao.Estado == Ignicoes.EstadoIgnicao.recusado)
                        {
                            ig.ListaOcorrencias.ElementAt(i).Estado = Ocorrencias.EstadoOcorrencia.recusado;
                        }

                    }

                }
    
                    ig.Latitude = ignicao.Latitude;
                    ig.Longitude = ignicao.Longitude;
                    ig.DataDecisaoIgnicao = dataDecisao;



            }
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!IgnicoesExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }
        }

        return NoContent();
    }

What I'm trying to do in the PUT method, is whenever the property "Estado" of an Ignicao is changed, simultaneously, the property Estado of every Ocorrencia in the property ListaOcorrencias is also changed.我试图在 PUT 方法中做的是,每当 Ignicao 的属性“Estado”发生变化时,同时,ListaOcorrencias 属性中每个 Ocorrencia 的属性 Estado 也会发生变化。 Right now the code that I show in the PUT method.现在是我在 PUT 方法中显示的代码。 It doesn't give me an error but when I tried to debbug it skips the code that it's inside the loop for.它没有给我一个错误,但是当我尝试调试时,它会跳过它在循环中的代码。 Why is this happening?为什么会这样?

From the comment above, the ig.ListaOcorrencias collection was not hydrated from the database.从上面的评论来看, ig.ListaOcorrencias集合没有从数据库中提取。 Lazy loading would need to be enabled for the above code to work.需要启用延迟加载才能使上述代码工作。 You could also eager load the related collection using Include like this: _context.Ignicoes.Include(i => i.ListaOccurrencias).FirstOrDefault(ignicaoId => ignicaoId.Id.Equals(id));您也可以像这样使用 Include _context.Ignicoes.Include(i => i.ListaOccurrencias).FirstOrDefault(ignicaoId => ignicaoId.Id.Equals(id));加载相关集合: _context.Ignicoes.Include(i => i.ListaOccurrencias).FirstOrDefault(ignicaoId => ignicaoId.Id.Equals(id)); , which is a good solution here since you always want to load the related collection. ,这是一个很好的解决方案,因为您总是想加载相关的集合。

Further reading:进一步阅读:

https://docs.microsoft.com/en-us/ef/core/querying/related-data https://docs.microsoft.com/en-us/ef/core/querying/related-data

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

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