Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. public class Artista
  2. {
  3.  
  4. public int ArtistaId { get; set; }
  5. public string Nome { get; set; }
  6. public string Email { get; set; }
  7. public string Site { get; set; }
  8. public string Descricao { get; set; }
  9.  
  10. public virtual Endereco Endereco { get; set; }
  11.  
  12. public DateTime DataCadastro { get; set; }
  13. public DateTime DataAtualizacao { get; set; }
  14.  
  15. public virtual ICollection<ArtistaCategoria> ArtistaCategoria { get; set; }
  16. }
  17.  
  18. public class Categoria
  19. {
  20. public Categoria()
  21. {
  22. }
  23.  
  24. public int CategoriaId { get; set; }
  25.  
  26. public string Nome { get; set; }
  27.  
  28. public virtual ICollection<ArtistaCategoria> ArtistaCategoria { get; set; }
  29. }
  30.  
  31. public class ArtistaCategoria
  32. {
  33. public int ArtistaCategoriaId { get; set; }
  34. public int ArtistaId { get; set; }
  35. public int CategoriaId { get; set; }
  36.  
  37. public virtual Artista Artista { get; set; }
  38. public virtual Categoria Categoria { get; set; }
  39. }
  40.  
  41. public class Endereco
  42. {
  43.  
  44. public Endereco()
  45. {
  46. Municipio = new Municipio();
  47. }
  48. public int EnderecoId { get; set; }
  49. public string Logradouro { get; set; }
  50. public string Numero { get; set; }
  51. public string Bairro { get; set; }
  52. public string Cep { get; set; }
  53. public int MunicipioId { get; set; }
  54. public virtual Municipio Municipio { get; set; }
  55.  
  56. }
  57.  
  58. public class Municipio
  59. {
  60. public Municipio()
  61. {
  62. }
  63.  
  64. public int MunicipioId { get; set; }
  65.  
  66. public string Nome { get; set; }
  67.  
  68. public string Cep { get; set; }
  69.  
  70. }
  71.  
  72. public void Remove(Artista artista)
  73. {
  74. var a = Db.Artistas.First(x => x.ArtistaId == artista.ArtistaId);
  75.  
  76. Db.Artistas.Attach(a);
  77.  
  78. RemoverTelefones(a);
  79. RemoverEndereco(a);
  80. RemoverCategorias(a);
  81.  
  82. Db.Set<Artista>().Remove(a);
  83. Db.SaveChanges();
  84. }
  85.  
  86.  
  87. private void RemoverTelefones(Artista artista)
  88. {
  89. // Telefones Originais
  90. var telefonesOriginais = Db.TelefoneArtista.AsNoTracking().Where(at => at.ArtistaId == artista.ArtistaId).ToList();
  91.  
  92. if (artista.ArtistaTelefones != null)
  93. {
  94. // Telefones Excluídos
  95. foreach (var telefoneOriginal in telefonesOriginais)
  96. {
  97. var telefoneExcluido = Db.TelefoneArtista.Single(rt => rt.TelefoneArtistaId == telefoneOriginal.TelefoneArtistaId);
  98. Db.TelefoneArtista.Remove(telefoneExcluido);
  99. Db.SaveChanges();
  100. }
  101.  
  102. }
  103. }
  104.  
  105. private void RemoverCategorias(Artista artista)
  106. {
  107.  
  108. var categorias = Db.ArtistaCategoria.AsNoTracking().Where(at => at.ArtistaId == artista.ArtistaId).ToList();
  109.  
  110. foreach (var categoria in categorias)
  111. {
  112. var catExcluida = Db.ArtistaCategoria.Single(rt => rt.ArtistaCategoriaId == categoria.ArtistaCategoriaId);
  113. Db.ArtistaCategoria.Remove(catExcluida);
  114. Db.SaveChanges();
  115. }
  116.  
  117. }
  118. private void RemoverEndereco(Artista artista)
  119. {
  120. var enderecos = Db.Enderecos.AsNoTracking().Where(at => at.EnderecoId == artista.Endereco.EnderecoId).ToList();
  121. artista.Endereco = null;
  122. foreach (var endereco in enderecos)
  123. {
  124. var endExcluido = Db.Enderecos.Single(rt => rt.EnderecoId == endereco.EnderecoId);
  125. Db.Enderecos.Remove(endExcluido);
  126. Db.SaveChanges();
  127.  
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement