4
I am having difficulty updating a record with entityframework, I will inform the whole structure below. In short, I have a register of artists, where these artists are related to categories.
entities:
Artist:
relationship one to one with address
many relationship to many with categories
public class Artista
{
public int ArtistaId { get; set; }
public string Nome { get; set; }
public string Email { get; set; }
public string Site { get; set; }
public string Descricao { get; set; }
public virtual Endereco Endereco { get; set; }
public DateTime DataCadastro { get; set; }
public DateTime DataAtualizacao { get; set; }
public virtual ICollection<ArtistaCategoria> ArtistaCategoria { get; set; }
}
public class Categoria
{
public Categoria()
{
}
public int CategoriaId { get; set; }
public string Nome { get; set; }
public virtual ICollection<ArtistaCategoria> ArtistaCategoria { get; set; }
}
public class ArtistaCategoria
{
public int ArtistaCategoriaId { get; set; }
public int ArtistaId { get; set; }
public int CategoriaId { get; set; }
public virtual Artista Artista { get; set; }
public virtual Categoria Categoria { get; set; }
}
public class Endereco
{
public Endereco()
{
Municipio = new Municipio();
}
public int EnderecoId { get; set; }
public string Logradouro { get; set; }
public string Numero { get; set; }
public string Bairro { get; set; }
public string Cep { get; set; }
public int MunicipioId { get; set; }
public virtual Municipio Municipio { get; set; }
}
public class Municipio
{
public Municipio()
{
}
public int MunicipioId { get; set; }
public string Nome { get; set; }
public string Cep { get; set; }
}
Configuration Fluent API
public class ArtistaConfiguration : EntityTypeConfiguration<Artista>
{
public ArtistaConfiguration()
{
HasKey(a => a.ArtistaId);
Property(a => a.Nome)
.IsRequired();
Property(a => a.Email)
.HasMaxLength(150);
}
public class EnderecoConfiguration : EntityTypeConfiguration<Endereco>
{
public EnderecoConfiguration()
{
HasKey(x => x.EnderecoId);
HasRequired(m => m.Municipio)
.WithMany()
.HasForeignKey(m => m.MunicipioId);
Property(m => m.Cep)
.IsFixedLength()
.HasMaxLength(9)
.HasColumnType("char");
}
}
When editing the category and artist relationship, an error is generated.
Attaching an Entity of type 'Showfacil.domain.Entities.Artist' failed because other Entity of the same type already has the same Primary key value. This can happen when using the 'Attach' method or Setting the state of an Entity to 'Unchanged' or 'Modified' if any entities in the Graph have Conflicting key values. This may be because some entities are new and have not yet Received database-generated key values. In this case use the 'Add' method or the 'Added' Entity state to track the Graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.
Follow the update code:
public void Update(Artista obj, string[] arrayCategoria)
{
AtualizaEndereco(obj);
ValidaCategorias(obj, arrayCategoria);
Db.Entry(obj).State = EntityState.Modified; << ERRO ACONTECE AQUI
Db.SaveChanges();
}
private void AtualizaEndereco(Artista artista)
{
if (artista.Endereco != null)
{
var endereco = artista.Endereco;
if (artista.Endereco.MunicipioId != 0)
{
var municipio = Db.Municipios.FirstOrDefault(x => x.MunicipioId == artista.Endereco.MunicipioId);
endereco.Municipio = municipio;
if (endereco.EnderecoId != 0)
{
Db.Entry(endereco).State = EntityState.Modified;
}
else
{
Db.Enderecos.Add(endereco);
}
artista.Endereco = endereco;
}
}
}
private void AtualizarCategorias(Artista artista, string[] categorias)
{
var artistaAtual = Db.Artistas
.FirstOrDefault(a => a.ArtistaId == artista.ArtistaId);
List<Categoria> categoriasSelecionadas = new List<Categoria>();
if (categorias != null)
{
foreach (var cat in categorias)
{
categoriasSelecionadas.Add(Db.Categorias.Find(int.Parse(cat)));
}
}
var categoriasOriginais = Db.ArtistaCategoria.Where(at => at.ArtistaId == artista.ArtistaId).ToList();
foreach (var item in categoriasOriginais)
{
Db.ArtistaCategoria.Remove(item);
Db.SaveChanges();
}
foreach (var categoria in categoriasSelecionadas)
{
var artistaCategoria = new ArtistaCategoria
{
Artista = artistaAtual,
Categoria = categoria
};
Db.ArtistaCategoria.Add(artistaCategoria);
Db.SaveChanges();
}
}
Okay, no mistake, but now always register a new artist..
– Alexandre Previatti
'Cause you’re wearing it
Add
.Add
always adds a new artist. Let to call the exchange ofState
outside the category validation method.– Leonel Sanches da Silva
did not understand... Leave to call the State exchange outside the method of validation of categories.
– Alexandre Previatti
I updated the answer.
– Leonel Sanches da Silva
Keeps adding a new artist...
– Alexandre Previatti
Can you tell me after which
SaveChanges
this duplicate insertion occurs?– Leonel Sanches da Silva
From saveChanges after var artistCategory = new Artistacategoria { Artist = artist, Category = category };
– Alexandre Previatti
I updated it once again.
– Leonel Sanches da Silva