public static int Delete(int id) { try { using (DVDCentralEntities dc = new DVDCentralEntities()) { //get the row i want to delete tblDirector deleteRow = (from dt in dc.tblDirectors where dt.Id == id select dt).FirstOrDefault(); if (deleteRow != null) { dc.tblDirectors.Remove(deleteRow); return(dc.SaveChanges()); } else { throw new Exception("Row not found"); } } } catch (Exception ex) { throw ex; } }
public static int Delete(int id) { try { using (DVDCentralEntities dc = new DVDCentralEntities()) { tblDirector deleterow = dc.tblDirectors.FirstOrDefault(dt => dt.Id == id); if (deleterow != null) { // Remove the row dc.tblDirectors.Remove(deleterow); // Commit/Save the changes return(dc.SaveChanges()); } else { throw new Exception("Row was not found."); } } } catch (Exception ex) { throw ex; } }
public static int Insert(Director director) { try { using (DVDCentralEntities dc = new DVDCentralEntities()) { tblDirector tblDirector = new tblDirector(); tblDirector.FirstName = director.FirstName; tblDirector.LastName = director.LastName; //example of ternary operator tblDirector.Id = dc.tblDirectors.Any() ? dc.tblDirectors.Max(dt => dt.Id) + 1 : 1; //Change the ID of the inserted Student director.Id = tblDirector.Id; dc.tblDirectors.Add(tblDirector); return(dc.SaveChanges()); } } catch (Exception ex) { throw ex; } }
public static int Update(Director director) { try { using (DVDCentralEntities dc = new DVDCentralEntities()) { //get the row i want to update tblDirector updateRow = (from dt in dc.tblDirectors where dt.Id == director.Id select dt).FirstOrDefault(); if (updateRow != null) { updateRow.FirstName = director.FirstName; updateRow.LastName = director.LastName; return(dc.SaveChanges()); } else { throw new Exception("Row not found"); } } } catch (Exception ex) { throw ex; } }
public static bool Insert(Director director) { try { using (DVDCentralEntities dc = new DVDCentralEntities()) { // Make a new row tblDirector newrow = new tblDirector(); // Set the properties // Ternary Operator condition ? true : false newrow.Id = dc.tblDirectors.Any() ? dc.tblDirectors.Max(p => p.Id) + 1 : 1; // If there are any rows, get the max id and add 1, if not use 1 newrow.FirstName = director.FirstName; newrow.LastName = director.LastName; // Do the Insert dc.tblDirectors.Add(newrow); // Commit the insert dc.SaveChanges(); return(true); } } catch (Exception ex) { throw ex; } }
public static int Update(Director director) { using (DVDCentralEntities dc = new DVDCentralEntities()) { try { tblDirector updatedrow = dc.tblDirectors.Where(dt => dt.Id == director.Id).FirstOrDefault(); // Check to see if object exists if (director != null) { // Update the proper fields updatedrow.FirstName = director.FirstName; updatedrow.LastName = director.LastName; // Save and commit changes return(dc.SaveChanges()); } else { // throw an exception stating the row was not found throw new Exception("Row was not found."); } } catch (Exception ex) { throw ex; } } }
public static Director LoadById(int id) { try { using (DVDCentralEntities dc = new DVDCentralEntities()) { tblDirector row = (from dt in dc.tblDirectors where dt.Id == id select dt).FirstOrDefault(); if (row != null) { return new Director { Id = row.Id, FirstName = row.FirstName, LastName = row.LastName } } ; else { throw new Exception("Row was not found"); } } } catch (Exception ex) { throw ex; } }
public void LoadById() { try { using (DVDEntities dc = new DVDEntities()) { //Make sure that the ID is set and valid if (this.Id >= 0) { tblDirector director = dc.tblDirectors.FirstOrDefault(d => d.Id == this.Id); //Make sure that a director was retrieved if (director != null) { //Set the properties on this director this.FirstName = director.FirstName; this.LastName = director.LastName; } else { throw new Exception("No director to load with this Id"); } } else { throw new Exception("Director Id not Valid"); } } } catch (Exception ex) { throw ex; } }
//Insert the director 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 tblDirector director = new tblDirector { Id = this.Id, FirstName = this.FirstName, LastName = this.LastName, Bio = this.Bio, BirthDate = this.BirthDate }; //Add it to the table and save changes dc.tblDirectors.Add(director); return(dc.SaveChanges()); } } catch (Exception ex) { throw ex; } }
public void Delete() { using (DVDEntities dc = new DVDEntities()) { tblDirector director = dc.tblDirectors.FirstOrDefault(d => d.Id == -99); dc.tblDirectors.Remove(director); int rowsEffected = dc.SaveChanges(); Assert.IsTrue(rowsEffected == 1); } }
public void Update() { using (DVDEntities dc = new DVDEntities()) { tblDirector director = dc.tblDirectors.FirstOrDefault(d => d.Id == -99); director.LastName = "TestingUpdate"; //Save changed property dc.SaveChanges(); tblDirector updatedDirector = dc.tblDirectors.FirstOrDefault(d => d.LastName == "TestingUpdate"); Assert.AreEqual(director.Id, updatedDirector.Id); } }
public void InsertTest() { tblDirector newrow = new tblDirector(); // Set the column values newrow.Id = -99; newrow.FirstName = "Ronald"; newrow.LastName = "McDonald"; // Insert of the row dc.tblDirectors.Add(newrow); // Commit the changes (insert a row) and then return the rows affected. int rowsaffected = dc.SaveChanges(); Assert.AreNotEqual(0, rowsaffected); }
public void InsertTest() { using (DVDCentralEntities dc = new DVDCentralEntities()) { tblDirector newrow = new tblDirector(); newrow.Id = -99; newrow.FirstName = "Johnny"; newrow.LastName = "Johnny"; dc.tblDirectors.Add(newrow); int results = dc.SaveChanges(); Assert.IsTrue(results == 1); } }
public static int Delete(int id) { try { using (DVDCentralEntities dc = new DVDCentralEntities()) { tblDirector deleterow = (from dt in dc.tblDirectors where dt.Id == id select dt).FirstOrDefault(); dc.tblDirectors.Remove(deleterow); return(dc.SaveChanges()); } } catch (Exception ex) { throw ex; } }
public void UpdateTest() { tblDirector existingDirector = (from dt in dc.tblDirectors where dt.Id == -99 select dt).FirstOrDefault(); if (existingDirector != null) { // Update the First and Last Name existingDirector.FirstName = "Ronald"; existingDirector.FirstName = "McDon"; dc.SaveChanges(); } tblDirector updatedDirector = (from dt in dc.tblDirectors where dt.Id == -99 select dt).FirstOrDefault(); Assert.AreEqual(existingDirector, updatedDirector); }
//Retrieve the director from the database with this Id public void LoadById() { try { using (MusicEntities dc = new MusicEntities()) { //Retrieve from the db tblDirector director = dc.tblDirectors.FirstOrDefault(p => p.Id == this.Id); //Set this director's properties this.FirstName = director.FirstName; this.LastName = director.LastName; this.Bio = director.Bio; this.BirthDate = director.BirthDate; } } catch (Exception ex) { throw ex; } }
public int Delete() { try { using (MusicEntities dc = new MusicEntities()) { //Retrieve it from the DB tblDirector director = dc.tblDirectors.FirstOrDefault(p => p.Id == this.Id); //Remove the director dc.tblDirectors.Remove(director); //Save the changes return(dc.SaveChanges()); } } catch (Exception ex) { throw ex; } }
public static int Update(int id, string firstName, string lastName) { try { using (DVDCentralEntities dc = new DVDCentralEntities()) { tblDirector updaterow = (from dt in dc.tblDirectors where dt.Id == id select dt).FirstOrDefault(); updaterow.FirstName = firstName; updaterow.LastName = lastName; return(dc.SaveChanges()); } } catch (Exception ex) { throw ex; } }
public void Insert() { using (DVDEntities dc = new DVDEntities()) { //Create a director tblDirector director = new tblDirector { Id = -99, FirstName = "TestDirector", LastName = "TestDirector" }; //Insert the row dc.tblDirectors.Add(director); //Commit the changes int rowsInserted = dc.SaveChanges(); //Make sure that one row was inserted Assert.IsTrue(rowsInserted == 1); } }
public void UpdateTest() { using (DVDCentralEntities dc = new DVDCentralEntities()) { //get row that I want to update //SELECT * gtp, tblDegreeType where id = -99 tblDirector row = (from dt in dc.tblDirectors where dt.Id == -99 select dt).FirstOrDefault(); //change the values to something else if (row != null) { row.FirstName = "Test"; row.LastName = "Tester"; } //save changes int results = dc.SaveChanges(); Assert.AreNotEqual(0, results); } }
public int Update() { try { using (DVDEntities dc = new DVDEntities()) { //Make sure that the ID is set and valid if (this.Id >= 0) { tblDirector director = dc.tblDirectors.FirstOrDefault(d => d.Id == this.Id); //Make sure that a director was retrieved if (director != null) { //Update the proterties on the director director.FirstName = this.FirstName; director.LastName = this.LastName; //return the number of rows effected return(dc.SaveChanges()); } else { throw new Exception("No director to load with this Id"); } } else { throw new Exception("Director Id not Valid"); } } } catch (Exception ex) { throw ex; } }
public static Director LoadById(int id) { try { if (id != 0) { using (DVDCentralEntities dc = new DVDCentralEntities()) { tblDirector tbldirector = dc.tblDirectors.FirstOrDefault(p => p.Id == id); if (tbldirector != null) { Director director = new Director { Id = tbldirector.Id, FirstName = tbldirector.FirstName, LastName = tbldirector.LastName }; return(director); } else { throw new Exception("Row was not found."); } } } else { throw new Exception("Please provide an id"); } } catch (Exception ex) { throw ex; } }
public int Insert() { try { using (DVDEntities dc = new DVDEntities()) { tblDirector director = new tblDirector { FirstName = this.FirstName, LastName = this.LastName, Id = dc.tblDirectors.Any() ? dc.tblDirectors.Max(d => d.Id) + 1 : 1 }; this.Id = director.Id; dc.tblDirectors.Add(director); //Return the rows effected return(dc.SaveChanges()); } } catch (Exception ex) { throw ex; } }
public static int Insert(out int id, string firstName, string lastName) { try { using (DVDCentralEntities dc = new DVDCentralEntities()) { tblDirector newrow = new tblDirector(); newrow.FirstName = firstName; newrow.LastName = lastName; newrow.Id = dc.tblDirectors.Any() ? dc.tblDirectors.Max(dt => dt.Id) + 1 : 1; id = newrow.Id; dc.tblDirectors.Add(newrow); return(dc.SaveChanges()); } } catch (Exception ex) { throw ex; } }
public int Update() { try { using (MusicEntities dc = new MusicEntities()) { //Retrieve the record from the DB tblDirector director = dc.tblDirectors.FirstOrDefault(c => c.Id == this.Id); //Update the properties director.FirstName = this.FirstName; director.LastName = this.LastName; director.Bio = this.Bio; director.BirthDate = this.BirthDate; //Save the changes return(dc.SaveChanges()); } } catch (Exception ex) { throw ex; } }