Update One to Many Entity Framework

Asked

Viewed 402 times

4

Hello, I need help with an update using Entity framework and relationship one for many. I believe it is something very basic, but I’m starting with EF and I can’t solve the problem.

Artist and Phoneartist entities, being that an artist can have multiple phones.

 public class Artista
    {

        public Artista()
        {
            Telefones = new List<TelefoneArtista>();
        }
        public int ArtistaId { get; set; }
        public string Nome { get; set; }
        public string Email { get; set; }
        public virtual ICollection<TelefoneArtista> Telefones { get; set; }
    }

public class TelefoneArtista
    {

        public int TelefoneArtistaId { get; set; }
        public string Numero { get; set; }
        public int ArtistaId { get; set; }
        public virtual Artista Artista { get; set; }

    }

How do I update?

I tried so but not so sure.

public void Update(Artista obj)
        {
            Db.Entry(obj).State = EntityState.Modified;
            Db.SaveChanges();
        }

Thanks in advance!!

1 answer

3


I use the following algorithm:

private async Task AtualizarTelefones(Artista artista)
{
    // Telefones Originais
    var telefonesOriginais = db.ArtistaTelefones.AsNoTracking().Where(at => at.ArtistaId == artista.ArtistaId).ToList();

    if (artista.ArtistaTelefones != null)
    {
        // Telefones Excluídos
        foreach (var telefoneOriginal in telefonesOriginais)
        {
            if (!artista.ArtistaTelefones.Any(rt => rt.ArtistaTelefoneId == telefoneOriginal.ArtistaTelefoneId))
            {
                var telefoneExcluido = db.ArtistaTelefones.Single(rt => rt.ArtistaTelefoneId == telefoneOriginal.ArtistaTelefoneId);
                db.ArtistaTelefones.Remove(telefoneExcluido);
                await db.SaveChangesAsync();
            }
        }

        // Telefones Inseridos ou Alterados
        foreach (var telefone in artista.ArtistaTelefones)
        {
            if (!telefonesOriginais.Any(rt => rt.ArtistaTelefoneId == telefone.ArtistaTelefoneId))
            {
                // Telefone não existe ainda. Inserir.
                telefone.ArtistaId = artista.ArtistaId;
                db.ArtistaTelefones.Add(telefone);
            }
            else
            {
                // Telefone já existe. Marcar como alterado.
                db.Entry(telefone).State = EntityState.Modified;
            }

            await db.SaveChangesAsync();
        }
    }
}

Use:

    public void Update(Artista obj)
    {
        await AtualizarTelefones(obj);
        Db.Entry(obj).State = EntityState.Modified;
        Db.SaveChanges();
    }
  • I am also learning EF, which if (!artista.ArtistaTelefones.Any(rt => rt.ArtistaTelefoneId == telefoneOriginal.ArtistaTelefoneId)) Do exactly, search if you have removed anything from the list?

  • 2

    This section checks whether there is no element ArtistaTelefoneId is equal to ArtistaTelefoneId that existed before. That is, the element was deleted on screen.

  • Additional information: It is not possible to start an asynchronous operation now. Asynchronous operations can only be initiated in an asynchronous manipulator or module or during certain events in the Page’s life cycle.

  • 1

    You can use synchronous code if you want. Just remove all async and await (including SaveChangesAsync, change to SaveChanges).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.