public int Delete() { try { using (MusicEntities dc = new MusicEntities()) { //Retrieve it from the DB tblPiece piece = dc.tblPieces.FirstOrDefault(p => p.Id == this.Id); //Remove the piece dc.tblPieces.Remove(piece); foreach (Genre genre in Genres) { genre.Delete(); } foreach (PieceWriter pieceWriter in PieceWriters) { pieceWriter.Delete(); } //Save the changes return(dc.SaveChanges()); } } catch (Exception ex) { throw ex; } }
//Retrieve the piece from the database with this Id public void LoadByName() { try { using (MusicEntities dc = new MusicEntities()) { //Retrieve from the db tblPiece piece = dc.tblPieces.FirstOrDefault(p => p.Name == this.Name); //Set this piece's properties this.Id = piece.Id; if (piece.YearWritten.HasValue) { this.YearWritten = piece.YearWritten.Value; } else { piece.YearWritten = -1; } this.GradeLevel = piece.GradeLevel; this.PerformanceNotes = piece.PefromanceNotes; LoadPieceWriters(); LoadGenres(); } } catch (Exception ex) { throw ex; } }
public int Update() { try { using (MusicEntities dc = new MusicEntities()) { //Retrieve the record from the DB tblPiece piece = dc.tblPieces.FirstOrDefault(p => p.Id == this.Id); //Update the properties piece.Id = this.Id; piece.Name = this.Name; piece.GradeLevel = this.GradeLevel; piece.PefromanceNotes = this.PerformanceNotes; //Set the year written if (this.YearWritten >= 0) { piece.YearWritten = this.YearWritten; } else { piece.YearWritten = null; } //Save the changes return(dc.SaveChanges()); } } catch (Exception ex) { throw ex; } }
public void DeleteTest() { using (MusicEntities dc = new MusicEntities()) { tblPiece piece = dc.tblPieces.FirstOrDefault(a => a.Name == "PL Test"); dc.tblPieces.Remove(piece); dc.SaveChanges(); tblPiece retrievedPiece = dc.tblPieces.FirstOrDefault(a => a.Name == "PL Test"); Assert.IsNull(retrievedPiece); } }
public void UpdateTest() { using (MusicEntities dc = new MusicEntities()) { tblPiece piece = dc.tblPieces.FirstOrDefault(a => a.Name == "PL Test"); piece.GradeLevel = "PL Updated Test 6"; dc.SaveChanges(); tblPiece retrievedPiece = dc.tblPieces.FirstOrDefault(a => a.Name == "PL Test"); Assert.IsNotNull(retrievedPiece); } }
public void InsertTest() { using (MusicEntities dc = new MusicEntities()) { tblPiece piece = new tblPiece(); piece.Id = Guid.NewGuid(); piece.Name = "PL Test"; piece.GradeLevel = "PL Test 4.5"; piece.YearWritten = 2019; piece.PefromanceNotes = "PL Test"; dc.tblPieces.Add(piece); dc.SaveChanges(); tblPiece retrievedPiece = dc.tblPieces.FirstOrDefault(a => a.Name == "PL Test"); Assert.AreEqual(piece.Id, retrievedPiece.Id); } }
//Insert the piece into the db, and generate a new Id for it. public int Insert() { try { using (MusicEntities dc = new MusicEntities()) { //Create a new Id this.Id = Guid.NewGuid(); //Set the properties tblPiece piece = new tblPiece { Id = this.Id, Name = this.Name, GradeLevel = this.GradeLevel, PefromanceNotes = this.PerformanceNotes, }; //Set the year written if (piece.YearWritten >= 0) { piece.YearWritten = this.YearWritten; } else { piece.YearWritten = null; } //Add it to the table and save changes dc.tblPieces.Add(piece); return(dc.SaveChanges()); } } catch (Exception ex) { throw ex; } }