//Insert the genre 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 tblGenre genre = new tblGenre { Id = this.Id, Description = this.Description }; //Add it to the table and save changes dc.tblGenres.Add(genre); return(dc.SaveChanges()); } } catch (Exception ex) { throw ex; } }
public static int Delete(int id) { try { using (DVDCentralEntities dc = new DVDCentralEntities()) { //get the row i want to delete tblGenre deleteRow = (from dt in dc.tblGenres where dt.Id == id select dt).FirstOrDefault(); if (deleteRow != null) { dc.tblGenres.Remove(deleteRow); return(dc.SaveChanges()); } else { throw new Exception("Row not found"); } } } catch (Exception ex) { throw ex; } }
public ActionResult FilterPostsByGenresID(int?id, int?page) { if (id == null) { return(HttpNotFound()); } tblGenre genre = db.tblGenres.Find(id); if (db.tblTags.FirstOrDefault(t => t.Tag_Title.Contains(genre.Genres_Name)) != null) { db.tblTags.FirstOrDefault(t => t.Tag_Title.Contains(genre.Genres_Name)).Tag_CountSearch++; db.SaveChanges(); } else { tblTag tag = new tblTag() { Tag_Title = genre.Genres_Name, Tag_DateAdd = DateTime.Now, Tag_CountSearch = 1 }; db.tblTags.Add(tag); db.SaveChanges(); } ViewBag.Title = "Tìm kiếm code theo thể loại"; ViewBag.countPost = db.tblPosts.Where(t => t.Post_Genres == id && t.Post_Active == true && t.Post_Trash == false).Count(); IPagedList <tblPost> post = db.tblPosts.Where(t => t.Post_Genres == id && t.Post_Active == true && t.Post_Trash == false).OrderByDescending(t => t.Post_DateCreate).ToPagedList(page ?? 1, PAGE_SIZE); return(View("Index", post)); }
public static bool Insert(Genre genre) { try { using (DVDCentralEntities dc = new DVDCentralEntities()) { // Make a new row tblGenre newrow = new tblGenre(); // Set the properties // Ternary Operator condition ? true : false newrow.Id = dc.tblGenres.Any() ? dc.tblGenres.Max(p => p.Id) + 1 : 1; // If there are any rows, get the max id and add 1, if not use 1 newrow.Description = genre.Description; // Do the Insert dc.tblGenres.Add(newrow); // Commit the insert dc.SaveChanges(); return(true); } } catch (Exception ex) { throw ex; } }
public static int Update(Genre genre) { try { using (DVDCentralEntities dc = new DVDCentralEntities()) { //get the row i want to update tblGenre updateRow = (from dt in dc.tblGenres where dt.Id == genre.Id select dt).FirstOrDefault(); if (updateRow != null) { updateRow.Description = genre.Description; return(dc.SaveChanges()); } else { throw new Exception("Row not found"); } } } catch (Exception ex) { throw ex; } }
public static int Update(Genre genre) { using (DVDCentralEntities dc = new DVDCentralEntities()) { try { tblGenre updatedrow = dc.tblGenres.Where(dt => dt.Id == genre.Id).FirstOrDefault(); // Check to see if object exists if (genre != null) { // Update the proper fields updatedrow.Description = genre.Description; // 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 int Delete(int id) { try { using (DVDCentralEntities dc = new DVDCentralEntities()) { tblGenre deleterow = dc.tblGenres.FirstOrDefault(dt => dt.Id == id); if (deleterow != null) { // Remove the row dc.tblGenres.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(Genre genre) { try { using (DVDCentralEntities dc = new DVDCentralEntities()) { tblGenre tblGenre = new tblGenre(); tblGenre.Description = genre.Description; //example of ternary operator tblGenre.Id = dc.tblGenres.Any() ? dc.tblGenres.Max(dt => dt.Id) + 1 : 1; //Change the ID of the inserted Student genre.Id = tblGenre.Id; dc.tblGenres.Add(tblGenre); return(dc.SaveChanges()); } } 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) { tblGenre genre = dc.tblGenres.FirstOrDefault(g => g.Id == this.Id); //Make sure that a genre was retrieved if (genre != null) { //Set the properties on this genre this.Description = genre.Description; } else { throw new Exception("No genre to load with this Id"); } } else { throw new Exception("Genre Id not Valid"); } } } catch (Exception ex) { throw ex; } }
public static Genre LoadById(int id) { try { using (DVDCentralEntities dc = new DVDCentralEntities()) { tblGenre row = (from dt in dc.tblGenres where dt.Id == id select dt).FirstOrDefault(); if (row != null) { return new Genre { Id = row.Id, Description = row.Description } } ; else { throw new Exception("Row was not found"); } } } catch (Exception ex) { throw ex; } }
public ActionResult DeleteConfirmed(int id) { tblGenre tblGenre = db.tblGenres.Find(id); db.tblGenres.Remove(tblGenre); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Edit([Bind(Include = "Genres_ID,Genres_Name")] tblGenre tblGenre) { if (ModelState.IsValid) { db.Entry(tblGenre).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(tblGenre)); }
public ActionResult Create([Bind(Include = "Genres_ID,Genres_Name")] tblGenre tblGenre) { if (ModelState.IsValid) { db.tblGenres.Add(tblGenre); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(tblGenre)); }
public void Delete() { using (DVDEntities dc = new DVDEntities()) { tblGenre genre = dc.tblGenres.FirstOrDefault(g => g.Id == -99); dc.tblGenres.Remove(genre); int rowsEffected = dc.SaveChanges(); Assert.IsTrue(rowsEffected == 1); } }
public ActionResult Edit([Bind(Include = "GenreID, GenreName, GenreBio, ImageSrc, ImageAlt")] tblGenre genre) { if (ModelState.IsValid) { context.Entry(genre).State = EntityState.Modified; context.SaveChanges(); return(RedirectToAction("AdminIndex")); } else { return(View()); } }
public void Update() { using (DVDEntities dc = new DVDEntities()) { tblGenre genre = dc.tblGenres.FirstOrDefault(g => g.Id == -99); genre.Description = "UpdateGenre"; //Save changed property dc.SaveChanges(); tblGenre updatedGenre = dc.tblGenres.FirstOrDefault(g => g.Description == "UpdateGenre"); Assert.AreEqual(genre.Id, updatedGenre.Id); } }
// GET: Admin/tblGenresAdmin/Delete/5 public ActionResult Delete(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } tblGenre tblGenre = db.tblGenres.Find(id); if (tblGenre == null) { return(HttpNotFound()); } return(View(tblGenre)); }
public void DeleteTest() { using (DVDCentralEntities dc = new DVDCentralEntities()) { tblGenre row = (from dt in dc.tblGenres where dt.Id == -99 select dt).FirstOrDefault(); dc.tblGenres.Remove(row); int results = dc.SaveChanges(); Assert.AreNotEqual(0, results); } }
public void InsertTest() { using (DVDCentralEntities dc = new DVDCentralEntities()) { tblGenre newrow = new tblGenre(); newrow.Id = -99; newrow.Description = "Children's Cartoon"; dc.tblGenres.Add(newrow); int results = dc.SaveChanges(); Assert.IsTrue(results == 1); } }
public void InsertTest() { tblGenre newrow = new tblGenre(); // Set the column values newrow.Id = -99; newrow.Description = "My new Genre"; // Insert of the row dc.tblGenres.Add(newrow); // Commit the changes (insert a row) and then return the rows affected. int rowsaffected = dc.SaveChanges(); Assert.AreNotEqual(0, rowsaffected); }
//Retrieve the genre from the database with this Id public void LoadById() { try { using (MusicEntities dc = new MusicEntities()) { //Retrieve from the db tblGenre genre = dc.tblGenres.FirstOrDefault(g => g.Id == this.Id); //Set this genre's properties this.Description = genre.Description; } } catch (Exception ex) { throw ex; } }
public static int Delete(int id) { try { using (DVDCentralEntities dc = new DVDCentralEntities()) { tblGenre deleterow = (from dt in dc.tblGenres where dt.Id == id select dt).FirstOrDefault(); dc.tblGenres.Remove(deleterow); return(dc.SaveChanges()); } } catch (Exception ex) { throw ex; } }
public static int Update(int id, string description) { try { using (DVDCentralEntities dc = new DVDCentralEntities()) { tblGenre updaterow = (from dt in dc.tblGenres where dt.Id == id select dt).FirstOrDefault(); updaterow.Description = description; return(dc.SaveChanges()); } } catch (Exception ex) { throw ex; } }
public int Update() { try { using (MusicEntities dc = new MusicEntities()) { //Retrieve the record from the DB tblGenre genre = dc.tblGenres.FirstOrDefault(g => g.Id == this.Id); //Update the properties genre.Description = this.Description; //Save the changes return(dc.SaveChanges()); } } catch (Exception ex) { throw ex; } }
public void Insert() { using (DVDEntities dc = new DVDEntities()) { //Create a genre tblGenre genre = new tblGenre { Id = -99, Description = "TestGenre" }; //Insert the row dc.tblGenres.Add(genre); //Commit the changes int rowsInserted = dc.SaveChanges(); //Make sure that one row was inserted Assert.IsTrue(rowsInserted == 1); } }
public ActionResult Create(CreateViewModel createvm) { if (ModelState.IsValid) { string filename = Path.GetFileName(createvm.Image.FileName); createvm.ImageSrc = "~/Content/GenreImages/" + filename; filename = Path.Combine(Server.MapPath("~/Content/GenreImages/"), filename); createvm.Image.SaveAs(filename); tblGenre genre = new tblGenre(createvm.GenreName, createvm.GenreBio, createvm.ImageSrc, createvm.ImageAlt); context.tblGenres.Add(genre); context.SaveChanges(); ModelState.Clear(); return(RedirectToAction("AdminIndex")); } else { return(View()); } }
public int Delete() { try { using (MusicEntities dc = new MusicEntities()) { //Retrieve it from the DB tblGenre genre = dc.tblGenres.FirstOrDefault(g => g.Id == this.Id); //Remove the genre dc.tblGenres.Remove(genre); //Save the changes return(dc.SaveChanges()); } } catch (Exception ex) { throw ex; } }
public int Insert() { try { using (DVDEntities dc = new DVDEntities()) { tblGenre genre = new tblGenre { Description = this.Description, Id = dc.tblGenres.Any() ? dc.tblGenres.Max(g => g.Id) + 1 : 1 }; this.Id = genre.Id; dc.tblGenres.Add(genre); //Return the rows effected return(dc.SaveChanges()); } } catch (Exception ex) { throw ex; } }
public ActionResult Delete(DeleteViewModel view) { bool isPasswordValid = context.tblUsers .Any(x => x.UserName.ToLower() == User.Identity.Name.ToLower() && x.Password == view.Password); if (isPasswordValid) { tblGenre genre = context.tblGenres.Find(view.Genre.GenreID); List <tblRecord> records = context.tblRecords.Where(x => x.ArtistID == genre.GenreID).ToList(); foreach (var item in records) { context.tblRecords.Remove(item); } context.tblGenres.Remove(genre); context.SaveChanges(); return(RedirectToAction("AdminIndex")); } return(View()); }
public static int Insert(out int id, string description) { try { using (DVDCentralEntities dc = new DVDCentralEntities()) { tblGenre newrow = new tblGenre(); newrow.Description = description; newrow.Id = dc.tblGenres.Any() ? dc.tblGenres.Max(dt => dt.Id) + 1 : 1; id = newrow.Id; dc.tblGenres.Add(newrow); return(dc.SaveChanges()); } } catch (Exception ex) { throw ex; } }