private void ExplicitLoading3()
        {
            //This is not allowed
            //db.Entry(album).Collection(t => t.Track.Where(f => f.Name.StartsWith("A"))).Load();

            using (ChinookContext db = new ChinookContext())
            {
                var Albums = db.Album.Take(5).ToList();

                foreach (var album in Albums)
                {
                    db.Entry(album).Collection(t => t.Track).Query().Where(f => f.Name.Contains("The")).Load();
                    db.Entry(album).Reference(t => t.Artist).Query().Load();

                    //Also works
                    //db.Entry(album).Collection(t => t.Track).Query().Where(f => f.Name.Contains("The")).ToList();
                    //db.Entry(album).Reference(t => t.Artist).Query().ToList();

                    Console.WriteLine("{0} {1}", album.Title, album.Artist.Name);

                    foreach (var track in album.Track)
                    {
                        Console.WriteLine("\t\t\t{0}", track.Name);
                    }
                }
            }

            Console.WriteLine("Press any key to continue");
            Console.ReadKey();
        }
        private void ExplicitLoading2()
        {
            using (ChinookContext db = new ChinookContext())
            {
                var Albums = db.Album.Take(5).ToList();

                foreach (var album in Albums)
                {
                    db.Entry(album).Collection(t => t.Track).Load();
                    db.Entry(album).Reference(t => t.Artist).Load();

                    Console.WriteLine("{0} {1}", album.Title, album.Artist.Name);

                    foreach (var track in album.Track)
                    {
                        Console.WriteLine("\t\t\t{0}", track.Name);
                    }
                }
            }

            Console.WriteLine("Press any key to continue");
            Console.ReadKey();


            //SELECT TOP(@__p_0) [a].[AlbumId], [a].[ArtistId], [a].[Title]
            //FROM [Album] AS [a]

            //SELECT [t].[TrackId], [t].[AlbumId], [t].[Bytes], [t].[Composer], [t].[GenreId], [t].[MediaTypeId], [t].[Milliseconds], [t].[Name], [t].[UnitPrice]
            //FROM [Track] AS [t]
            //WHERE [t].[AlbumId] = @__p_0

            //SELECT [a].[ArtistId], [a].[Name]
            //FROM [Artist] AS [a]
            //WHERE [a].[ArtistId] = @__p_0
        }
        public async Task <IActionResult> PutTracks([FromRoute] long id, [FromBody] Tracks tracks)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != tracks.TrackId)
            {
                return(BadRequest());
            }

            _context.Entry(tracks).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TracksExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
예제 #4
0
        public async Task <IActionResult> PutArtista(int id, Artista artista)
        {
            if (id != artista.ArtistaId)
            {
                return(BadRequest());
            }

            _context.Entry(artista).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ArtistaExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
예제 #5
0
        public async Task <IActionResult> PutCancion(int id, Cancion cancion)
        {
            if (id != cancion.CancionId)
            {
                return(BadRequest());
            }

            _context.Entry(cancion).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CancionExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
예제 #6
0
        //// Daten anlegen
        static void Main(string[] args)
        {
            using (ChinookContext context = new ChinookContext())
            {
                context.Database.Log = Console.WriteLine;

                Artist artist = new Artist()
                {
                    Name = "Hosen"
                };
                //Album album = new Album() { Artist = artist, Title = "Atmen" };

                artist.Albums.Add(new Album()
                {
                    Title = "Gürtelrose"
                });

                Console.WriteLine(context.Entry(artist).State);

                // Neu erzeugtes Element dem Context hinzufügen
                context.Artists.Add(artist);
                //context.Albums.Add(album);

                context.SaveChanges();

                //Console.WriteLine(context.Entry(artist).State);
            }

            Console.ReadKey();
        }
예제 #7
0
        public async Task <IActionResult> PutAlbum([FromRoute] int id, [FromBody] Album album)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != album.AlbumId)
            {
                return(BadRequest());
            }

            _context.Entry(album).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AlbumExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
예제 #8
0
        }//eom

        public void DeleteTracks(string username, string playlistname, List <int> trackstodelete)
        {
            using (var context = new ChinookContext())
            {
                Playlist exists = (from x in context.Playlists
                                   where x.UserName.Equals(username, StringComparison.OrdinalIgnoreCase) &&
                                   x.Name.Equals(playlistname, StringComparison.OrdinalIgnoreCase)
                                   select x).FirstOrDefault();
                if (exists == null)
                {
                    throw new Exception("Playlist has been removed from the database.");
                }
                else
                {
                    var           trackskept = exists.PlaylistTracks.Where(tr => !trackstodelete.Any(tod => tod == tr.TrackId)).Select(tr => tr);
                    PlaylistTrack item       = null;

                    foreach (int dtrack in trackstodelete)
                    {
                        item = exists.PlaylistTracks.Where(dx => dx.TrackId == dtrack).FirstOrDefault();
                        exists.PlaylistTracks.Remove(item);
                    }
                    int newrownumber = 1;
                    foreach (var trackkept in trackskept)
                    {
                        trackkept.TrackNumber = newrownumber;
                        context.Entry(trackkept).Property(y => y.TrackNumber).IsModified = true;
                        newrownumber++;
                    }
                    context.SaveChanges();
                }
            }
        }//eom
예제 #9
0
        public async Task <IActionResult> PutGenero(int id, Genero genero)
        {
            if (id != genero.GeneroId)
            {
                return(BadRequest());
            }

            _context.Entry(genero).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!GeneroExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
예제 #10
0
 public void UpdateTrack(Track info)
 {
     using (var context = new ChinookContext())
     {
         var existing = context.Entry(info);
         existing.State = EntityState.Modified;
         context.SaveChanges();
     }
 }
예제 #11
0
 public void UpdateEmployee(Employee info)
 {
     using (var context = new ChinookContext())
     {
         DbEntityEntry <Employee> existing = context.Entry(info);
         existing.State = System.Data.Entity.EntityState.Modified;
         context.SaveChanges();
     }
 }
예제 #12
0
        public async Task <ActionResult> Edit([Bind(Include = "AlbumId,Title,ArtistId")] Album album)
        {
            if (ModelState.IsValid)
            {
                db.Entry(album).State = EntityState.Modified;
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
            return(View(album));
        }
예제 #13
0
        public int Album_Update(Album item)
        {
            using (var context = new ChinookContext())
            {
                if (CheckReleaseYear(item))
                {
                    //any additional logic
                    item.ReleaseLabel = string.IsNullOrEmpty(item.ReleaseLabel) ? null :
                                        item.ReleaseLabel;

                    context.Entry(item).State = System.Data.Entity.EntityState.Modified; //staging
                    return(context.SaveChanges());                                       //commit to database AND RETURN ROWS AFFECTED.
                }
                else
                {
                    throw new BusinessRuleException("Validation Error", reasons);
                }
            }
        }
예제 #14
0
        public void Albums_Update(Album item)
        {
            using (var context = new ChinookContext())
            {
                //any business rules

                //any data refinements
                //review of using iif
                //composer cam be a mi;; stromg
                //we do not wish to store an empty string
                context.Albums.Attach(item);
                item.ReleaseLabel = string.IsNullOrEmpty(item.ReleaseLabel) ? null : item.ReleaseLabel;

                //update the existing instance of truckinfo on the database
                context.Entry(item).State = EntityState.Modified;

                //update command if updating selected fields
                //context
                context.SaveChanges();
            }
        }
예제 #15
0
        public virtual async Task <bool> Modify(long id, TEntity entity)
        {
            _context.Entry(entity).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!EntityExists(id))
                {
                    return(false);
                }
                else
                {
                    throw;
                }
            }

            return(true);
        }
예제 #16
0
 public void MoveTrack(string username, string playlistname, int trackid, int tracknumber, string direction)
 {
     using (var context = new ChinookContext())
     {
         Playlist exists = (from x in context.Playlists
                            where x.UserName.Equals(username, StringComparison.OrdinalIgnoreCase) &&
                            x.Name.Equals(playlistname, StringComparison.OrdinalIgnoreCase)
                            select x).FirstOrDefault();
         if (exists == null)
         {
             throw new Exception("Playlist has been removed from the database.");
         }
         else
         {
             PlaylistTrack movetrack = (from x in exists.PlaylistTracks
                                        where x.TrackId == trackid
                                        select x).FirstOrDefault();
             if (movetrack == null)
             {
                 throw new Exception("Primary Playlist track has been removed from the database.");
             }
             else
             {
                 PlaylistTrack othertrack = null;
                 if (direction.Equals("UP"))
                 {
                     othertrack = (from x in exists.PlaylistTracks
                                   where x.TrackNumber == tracknumber - 1
                                   select x).FirstOrDefault();
                     if (othertrack == null)
                     {
                         throw new Exception("Secondary Playlist track has been removed from the database.");
                     }
                     else
                     {
                         movetrack.TrackNumber  -= 1;
                         othertrack.TrackNumber += 1;
                     }
                 }
                 else
                 {
                     othertrack = (from x in exists.PlaylistTracks
                                   where x.TrackNumber == tracknumber + 1
                                   select x).FirstOrDefault();
                     if (othertrack == null)
                     {
                         throw new Exception("Secondary Playlist track has been removed from the database.");
                     }
                     else
                     {
                         movetrack.TrackNumber  += 1;
                         othertrack.TrackNumber -= 1;
                     }
                 }
                 context.Entry(movetrack).Property(y => y.TrackNumber).IsModified  = true;
                 context.Entry(othertrack).Property(y => y.TrackNumber).IsModified = true;
                 context.SaveChanges();
             }
         }
     }
 }//eom