public int AddEmp(EmpModel model) { using (var context = new AniketEntities()) { tblEmp emp = new tblEmp() { FirstName = model.FirstName, LastName = model.LastName, Email = model.Email, Code = model.Code }; if (model.Address != null) { emp.tblAddress = new tblAddress() { Details = model.Address.Details, Country = model.Address.Country, State = model.Address.State, City = model.Address.City, }; } context.tblEmp.Add(emp); context.SaveChanges(); return(emp.Id); } }
public ActionResult DeleteConfirmed(int id) { tblEmp tblEmp = db.tblEmps.Find(id); db.tblEmps.Remove(tblEmp); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Edit([Bind(Include = "EmpID,Name,City,MobileNo,birthdate,Email")] tblEmp tblEmp) { if (ModelState.IsValid) { db.Entry(tblEmp).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(tblEmp)); }
public ActionResult Create([Bind(Include = "EmpID,Name,City,MobileNo,birthdate,Email")] tblEmp tblEmp) { if (ModelState.IsValid) { db.tblEmps.Add(tblEmp); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(tblEmp)); }
// GET: tblEmps/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } tblEmp tblEmp = db.tblEmps.Find(id); if (tblEmp == null) { return(HttpNotFound()); } return(View(tblEmp)); }
public bool UpdateEmp(int id, EmpModel model) { using (var context = new AniketEntities()) { var emp = new tblEmp(); //context.tblEmp.FirstOrDefault(x => x.Id == id); emp.Id = model.Id; emp.FirstName = model.FirstName; emp.LastName = model.LastName; emp.Email = model.Email; emp.Code = model.Code; emp.AddressId = model.AddressId; context.Entry(emp).State = System.Data.Entity.EntityState.Modified; //most imp line context.SaveChanges(); return(true); }; }
public bool DeleteEmp(int id) { using (var context = new AniketEntities()) { //For double hit database operation in delete method first get record then deleted //var emp = context.tblEmp.FirstOrDefault(x => x.Id == id); //if (emp != null) //{ // context.tblEmp.Remove(emp); // context.SaveChanges(); // return true; //} //For single hit database operation in delete method var emp = new tblEmp() { Id = id }; context.Entry(emp).State = System.Data.Entity.EntityState.Deleted; //most imp line context.SaveChanges(); return(true); } }
//POST api/employee public void PostEmployee(tblEmp Employee) { objDataContext.tblEmps.InsertOnSubmit(Employee); objDataContext.SubmitChanges(); }