Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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; }
- }
- public void Remove(Artista artista)
- {
- var a = Db.Artistas.First(x => x.ArtistaId == artista.ArtistaId);
- Db.Artistas.Attach(a);
- RemoverTelefones(a);
- RemoverEndereco(a);
- RemoverCategorias(a);
- Db.Set<Artista>().Remove(a);
- Db.SaveChanges();
- }
- private void RemoverTelefones(Artista artista)
- {
- // Telefones Originais
- var telefonesOriginais = Db.TelefoneArtista.AsNoTracking().Where(at => at.ArtistaId == artista.ArtistaId).ToList();
- if (artista.ArtistaTelefones != null)
- {
- // Telefones Excluídos
- foreach (var telefoneOriginal in telefonesOriginais)
- {
- var telefoneExcluido = Db.TelefoneArtista.Single(rt => rt.TelefoneArtistaId == telefoneOriginal.TelefoneArtistaId);
- Db.TelefoneArtista.Remove(telefoneExcluido);
- Db.SaveChanges();
- }
- }
- }
- private void RemoverCategorias(Artista artista)
- {
- var categorias = Db.ArtistaCategoria.AsNoTracking().Where(at => at.ArtistaId == artista.ArtistaId).ToList();
- foreach (var categoria in categorias)
- {
- var catExcluida = Db.ArtistaCategoria.Single(rt => rt.ArtistaCategoriaId == categoria.ArtistaCategoriaId);
- Db.ArtistaCategoria.Remove(catExcluida);
- Db.SaveChanges();
- }
- }
- private void RemoverEndereco(Artista artista)
- {
- var enderecos = Db.Enderecos.AsNoTracking().Where(at => at.EnderecoId == artista.Endereco.EnderecoId).ToList();
- artista.Endereco = null;
- foreach (var endereco in enderecos)
- {
- var endExcluido = Db.Enderecos.Single(rt => rt.EnderecoId == endereco.EnderecoId);
- Db.Enderecos.Remove(endExcluido);
- Db.SaveChanges();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement