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!!
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?– rubStackOverflow
This section checks whether there is no element
ArtistaTelefoneId
is equal toArtistaTelefoneId
that existed before. That is, the element was deleted on screen.– Leonel Sanches da Silva
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.
– Alexandre Previatti
You can use synchronous code if you want. Just remove all
async
andawait
(includingSaveChangesAsync
, change toSaveChanges
).– Leonel Sanches da Silva