Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.74 KB | None | 0 0
  1. public void Remove<TObject>(TObject @object) where TObject : class
  2.         {
  3.             if (@object is Album album)
  4.             {
  5.                 if (album.Pictures.Any()) Remove(album.Pictures.ToList());
  6.  
  7.                 _context.Albums.Attach(album);
  8.                 _context.Entry(album).State = EntityState.Deleted;
  9.             }
  10.             else if (@object is Photo photo)
  11.             {
  12.                 _context.Photos.Attach(photo);
  13.                 _context.Entry(photo).State = EntityState.Deleted;
  14.  
  15.             }else if(@object is Author author)
  16.             {
  17.                 _context.Authors.Attach(author);
  18.                 _context.Entry(author).State = EntityState.Deleted;
  19.             }
  20.             else if(@object is Genre genre)
  21.             {
  22.                 _context.Genres.Attach(genre);
  23.                 _context.Entry(genre).State = EntityState.Deleted;
  24.             }
  25.             else if(@object is Filter filter)
  26.             {
  27.                 _context.Filters.Attach(filter);
  28.                 _context.Entry(filter).State = EntityState.Deleted;
  29.             }
  30.             else if(@object is IEnumerable<Photo> photos)
  31.             {
  32.                 foreach (Photo p in photos)
  33.                 {
  34.                     _context.Photos.Attach(p);
  35.                     _context.Entry(p).State = EntityState.Deleted;
  36.                 }
  37.  
  38.             }else if(@object is IEnumerable<Album> albums)
  39.             {
  40.                 foreach (Album a in albums)
  41.                 {
  42.                     if (a.Pictures.Any()) Remove(a.Pictures);
  43.  
  44.                     _context.Albums.Attach(a);
  45.                     _context.Entry(a).State = EntityState.Deleted;
  46.                 }
  47.             }
  48.             else if(@object is IEnumerable<Author> authors)
  49.             {
  50.                 foreach (Author auth in authors)
  51.                 {
  52.                     _context.Authors.Attach(auth);
  53.                     _context.Entry(auth).State = EntityState.Deleted;
  54.                 }
  55.             }
  56.             else if(@object is IEnumerable<Filter> filters)
  57.             {
  58.                 foreach (Filter fil in filters)
  59.                 {
  60.                     _context.Filters.Attach(fil);
  61.                     _context.Entry(fil).State = EntityState.Deleted;
  62.                 }
  63.             }
  64.             else if(@object is IEnumerable<Genre> genres)
  65.             {
  66.                 foreach (Genre gen in genres)
  67.                 {
  68.                     _context.Genres.Attach(gen);
  69.                     _context.Entry(gen).State = EntityState.Deleted;
  70.                 }
  71.             }
  72.             else
  73.             {
  74.                 PhotoAlbumTools.UnknownEntity(@object);
  75.                 return;
  76.             }
  77.  
  78.             _context.SaveChanges();
  79.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement