public ActionResult Create([Bind(Include = "Id,Name")] empDB empDB) { if (ModelState.IsValid) { db.empDBs.Add(empDB); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(empDB)); }
public HttpResponseMessage Delete(int id) { try { using (EmployeeDBEntities1 entities = new EmployeeDBEntities1()) { var entity = entities.Employees.FirstOrDefault(e => e.ID == id); if (entity == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with Id = " + id.ToString() + " not found to delete")); } else { entities.Employees.Remove(entity); entities.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK)); } } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public HttpResponseMessage Put(int id, [FromBody] Employee employee) { try { using (EmployeeDBEntities1 entities = new EmployeeDBEntities1()) { var entity = entities.Employees.FirstOrDefault(e => e.ID == id); if (entity == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with Id = " + id.ToString() + " has no record ")); } else { entity.FirstName = employee.FirstName; entity.LastName = employee.LastName; entity.Gender = employee.Gender; entity.Salary = employee.Salary; entities.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, entity)); } } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public void CreateEmployee(Employee employee) { if (employee != null) { _employeeDBEntities.Employees.Add(employee); _employeeDBEntities.SaveChanges(); } }
public IHttpActionResult DeleteCustomer(int id) { var db = new EmployeeDBEntities1(); var customer = new Customer { Id = id }; db.Customers.Attach(customer); db.Customers.Remove(customer); db.SaveChanges(); return(Ok(id)); }
public IHttpActionResult SaveCustomer(Customer customer) { if (customer.Id > 0) { var db = new EmployeeDBEntities1(); var dbCustomer = db.Customers.FirstOrDefault(x => x.Id == customer.Id); dbCustomer.Name = customer.Name; dbCustomer.Address = customer.Address; dbCustomer.Phone = customer.Phone; db.SaveChanges(); } else { var db = new EmployeeDBEntities1(); db.Customers.Add(customer); db.SaveChanges(); } return(Ok(customer.Id)); }
public HttpResponseMessage Post([FromBody] Employee employee) { try { using (EmployeeDBEntities1 entities = new EmployeeDBEntities1()) { entities.Employees.Add(employee); entities.SaveChanges(); var message = Request.CreateResponse(HttpStatusCode.Created, employee); message.Headers.Location = new Uri(Request.RequestUri + employee.ID.ToString()); return(message); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }