public ActionResult Create([Bind(Include = "Name,Price")] Product product) { if (ModelState.IsValid) { db.Products.Add(product); db.SaveChanges(); return(Redirect("~/Umbraco/Surface/Products/Index")); //return RedirectToAction("Index"); } return(View(product)); }
public ActionResult Create([Bind(Include = "Id,Name,Dob,Salary,CreateUser,CreateDate,UpdateUser,UpdateDate")] Employee employee) { if (ModelState.IsValid) { db.Employees.Add(employee); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(employee)); }
public ActionResult Create([Bind(Include = "Id,ProductName,SupplierId,UnitPrice,Package,IsDiscontinued")] Product product) { if (ModelState.IsValid) { db.Products.Add(product); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.SupplierId = new SelectList(db.Suppliers, "Id", "CompanyName", product.SupplierId); return(View(product)); }
public HttpResponseMessage Post([FromBody] Product product) { try { if (product != null) { using (var db = new DemoDbEntities()) { product.ProductID = Guid.NewGuid(); db.Products.Add(product); db.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.Created)); } } else { return(Request.CreateResponse("Operation can't be done")); } } catch (Exception e) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e)); } }
public void DeleteUser(int id) { using (DemoDbEntities db = new DemoDbEntities()) { db.Users.Remove(db.Users.Where(z => z.Id == id).FirstOrDefault()); db.SaveChanges(); } }
public void DeleteDepartment(int id) { using (DemoDbEntities db = new DemoDbEntities()) { db.Departments.Remove(db.Departments.FirstOrDefault(z => z.Id == id)); db.SaveChanges(); } }
public HttpResponseMessage PostProduct([FromBody] Product product) { using (var db = new DemoDbEntities()) { db.Products.Add(product); db.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK)); // return Request.CreateResponse(HttpStatusCode.Created); } }
public void UpdateDepartment(DepartmentPocos department) { using (DemoDbEntities db = new DemoDbEntities()) { Department olddepartment = db.Departments.Find(department.Id); olddepartment.Address = department.Address; olddepartment.Description = department.Description; olddepartment.Active = department.active; db.SaveChanges(); } }
public void UpdateUser(UserPocos user) { using (DemoDbEntities db = new DemoDbEntities()) { User olduser = db.Users.FirstOrDefault(z => z.Id == user.Id); olduser.FirstName = user.FirstName; olduser.LastName = user.LastName; olduser.Country = user.Country; olduser.Address = user.Address; olduser.DepartmentId = user.DepartmentId; olduser.Description = user.Description; olduser.Active = user.Active; db.SaveChanges(); } }
public HttpResponseMessage Put(Guid?id, [FromBody] Product product) { var p = db.Products.Find(id); try { if (p != null) { p.Name = product.Name; p.Price = product.Price; db.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK)); } else { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Id = " + id.ToString() + " was not found..")); } } catch (Exception e) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e)); } }
public int AddDepartment(DepartmentPocos department) { using (DemoDbEntities db = new DemoDbEntities()) { Department newdepartment = new Department() { Description = department.Description, Address = department.Address, Active = department.active }; db.Departments.Add(newdepartment); db.SaveChanges(); return(newdepartment.Id); } }
public int AddUser(UserPocos user) { using (DemoDbEntities db = new DemoDbEntities()) { User newUser = new User() { FirstName = user.FirstName, LastName = user.LastName, Address = user.Address, Description = user.Description, DepartmentId = user.DepartmentId, Country = user.Country, Active = user.Active, }; db.Users.Add(newUser); db.SaveChanges(); return(newUser.Id); } }
private void metroButtonSave_Click(object sender, EventArgs e) { int deptId = Convert.ToInt32(metroLabelDepatmentId.Text); //some validation can be added over here, like check duplicate department id if (!IsValidDeptTitle(metroTextBoxDepartmentTitle.Text)) { MetroMessageBox.Show(this, "Department Already Exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } using (var context = new DemoDbEntities()) { if (deptId == 0) { Department dept = new Department() { Title = metroTextBoxDepartmentTitle.Text }; context.Departments.Add(dept); } else { var deptToUpdate = context.Departments.SingleOrDefault(dept => dept.Id == deptId); if (deptToUpdate != null) { deptToUpdate.Title = metroTextBoxDepartmentTitle.Text; } } //can use try catch block over here to catch any error while saving context.SaveChanges(); } //show success message MetroMessageBox.Show(this, "Department Saved Successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); //reresh grid InitializeDepartmentList(); }
/// <summary> /// Save method. /// </summary> public void Save() { try { _context.SaveChanges(); } catch (DbEntityValidationException e) { var outputLines = new List <string>(); foreach (var eve in e.EntityValidationErrors) { outputLines.Add(string.Format( "{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:", DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State)); foreach (var ve in eve.ValidationErrors) { outputLines.Add(string.Format("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage)); } } System.IO.File.AppendAllLines(@"C:\errors.txt", outputLines); throw e; } }
private void metroButtonSave_Click(object sender, EventArgs e) { int empId = Convert.ToInt32(metroLabelEmployeeId.Text); using (var context = new DemoDbEntities()) { if (empId == 0) { //create an employee object Employee employee = new Employee { Name = metroTextEmployeeName.Text, DeptId = Convert.ToInt32(metroComboBoxDepartment.SelectedValue) }; context.Employees.Add(employee); } else { var empToUpdate = context.Employees.SingleOrDefault(emp => emp.Id == empId); if (empToUpdate != null) { empToUpdate.Name = metroTextEmployeeName.Text; empToUpdate.DeptId = Convert.ToInt32(metroComboBoxDepartment.SelectedValue); } } //can use try catch block over here to catch any error while saving context.SaveChanges(); } //show success message MetroMessageBox.Show(this, "Employee Saved Successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); //reresh grid LoadEmployees(); }
static void Main(string[] args) { bool flag = true; int choice; var dbContext = new DemoDbEntities(); while (flag) { Console.WriteLine("Select an option you want to perform:"); Console.WriteLine("1. Add Movie"); Console.WriteLine("2. Delete Movie by name"); Console.WriteLine("3. List of Movies"); Console.WriteLine("4. Add Actor"); Console.WriteLine("5. Delete Actor by name"); Console.WriteLine("6. List of Actors"); Console.WriteLine("7. Exit"); Console.WriteLine("Enter your choice"); choice = Convert.ToInt32(Console.ReadLine()); switch (choice) { case 1: Console.WriteLine("Enter Movie Name : "); string name = Console.ReadLine(); var check = dbContext.Movies.Where(t => t.MovieName == name); Console.WriteLine(check.Count()); if (check.Count() >= 1) { Console.WriteLine("Movie is already in the list !!"); } else { var movie = new Movy() { MovieName = name }; try { dbContext.Movies.Add(movie); dbContext.SaveChanges(); Console.WriteLine("Movie is added !!"); } catch (Exception e) { Console.WriteLine(e); } } break; case 2: Console.WriteLine("Enter movie name to delete : "); string del = Console.ReadLine(); var delMovie = dbContext.Movies.Where(t => t.MovieName == del); if (delMovie.Count() == 1) { try { dbContext.Movies.Remove(dbContext.Movies.Single(t => t.MovieName == del)); dbContext.SaveChanges(); Console.WriteLine("Movie is removed !!"); } catch (Exception e) { Console.WriteLine(e); } } else { Console.WriteLine("Movie is not available in the list"); } break; case 3: var movieList = dbContext.Movies; try { foreach (var movy in movieList) { Console.WriteLine("Movie Name : " + movy.MovieName); } } catch (Exception e) { Console.WriteLine(e); } break; case 4: Console.WriteLine("Enter Actor Name : "); string Aname = Console.ReadLine(); var checkActor = dbContext.Actors.Where(t => t.ActorName == Aname); if (checkActor.Count() >= 1) { Console.WriteLine("Actor is already in the list !!"); } else { var actor = new Actor() { ActorName = Aname }; try { dbContext.Actors.Add(actor); dbContext.SaveChanges(); Console.WriteLine("Actor is added !!"); } catch (Exception e) { Console.WriteLine(e); } } break; case 5: Console.WriteLine("Enter Actor name to delete : "); string delA = Console.ReadLine(); var delActor = dbContext.Actors.Where(t => t.ActorName == delA); if (delActor.Count() == 1) { try { dbContext.Actors.Remove(dbContext.Actors.Single(t => t.ActorName == delA)); dbContext.SaveChanges(); Console.WriteLine("Actor is removed !!"); } catch (Exception e) { Console.WriteLine(e); } } else { Console.WriteLine("Actor is not available in the list"); } break; case 6: var actorList = dbContext.Actors; try { foreach (var actor in actorList) { Console.WriteLine("Actor Name : " + actor.ActorName); } } catch (Exception e) { Console.WriteLine(e); } break; case 7: flag = false; break; default: break; } } }
public async Task SaveChanges() { await Task.Run(() => _Context.SaveChanges()); }