public Person Add(Person person) { using (var Transaction = db.Database.BeginTransaction()) { try { db.Person.Add(person); db.SaveChanges(); var institute = new Institute() { Name = "Institute Name", PersonID = person.Id }; db.Institute.Add(institute); db.SaveChanges(); person.InstituteID = institute.Id; db.Entry(person).Property(x => x.InstituteID).IsModified = true; db.SaveChanges(); Transaction.Commit(); } catch (Exception ex) { Transaction.Rollback(); } } return(person); }
//public void Update(T entity, int updated_by = 0) //{ // //TrySetProperty(entity, "updated_by", created_by); // //TrySetProperty(entity, "update_date", DateTime.UtcNow); // db.Entry<T>(entity).State = EntityState.Modified; // db.SaveChanges(); //} public virtual void Update(T entity, params Expression <Func <T, object> >[] updatedProperties) { //dbEntityEntry.State = EntityState.Modified; --- I cannot do this. ds.Attach(entity); //Ensure only modified fields are updated. var dbEntityEntry = db.Entry(entity); if (updatedProperties.Any()) { //update explicitly mentioned properties foreach (var property in updatedProperties) { dbEntityEntry.Property(property).IsModified = true; } } else { //no items mentioned, so find out the updated entries foreach (var property in dbEntityEntry.OriginalValues.PropertyNames) { var original = dbEntityEntry.OriginalValues.GetValue <object>(property); var current = dbEntityEntry.CurrentValues.GetValue <object>(property); if (original != null && !original.Equals(current)) { dbEntityEntry.Property(property).IsModified = true; } } } db.SaveChanges(); }
public void Update(Institute entity) { db.Institute.Attach(entity); db.Entry(entity).Property(x => x.Address).IsModified = true; db.Entry(entity).Property(x => x.Email).IsModified = true; db.Entry(entity).Property(x => x.Name).IsModified = true; db.Entry(entity).Property(x => x.Phone).IsModified = true; if (entity.Logo != null) { db.Entry(entity).Property(x => x.Logo).IsModified = true; } db.SaveChanges(); //if (entity.Logo == null) // rep.Update(entity, x => x.Address, x => x.Email, x => x.Name, x => x.Phone); //else // rep.Update(entity, x => x.Address, x => x.Email, x => x.Name, x => x.Phone, x => x.Logo); }
public ActionResult Edit([Bind(Include = "Id,Name,AddressID,Deleted")] Address address) { if (ModelState.IsValid) { db.Entry(address).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.AddressID = new SelectList(db.Address, "Id", "Name", address.AddressID); return(View(address)); }
public ActionResult Edit([Bind(Include = "Id,TotalMarks")] ClassExams ClassExams) { if (ModelState.IsValid) { var instuteID = Convert.ToInt32(Membership.GetUser().PasswordQuestion); ClassExams Exams = db.ClassExams.Find(ClassExams.Id); if (Exams == null || Convert.ToInt32(Exams.InstituteID) != instuteID || Exams.Deleted == true) { return(HttpNotFound()); } if (!rep.CheckIfExists(Exams.ClassID, Exams.ExamNamesID)) { ModelState.AddModelError(string.Empty, "Data Doesn't Exists"); return(View(ClassExams)); } Exams.TotalMarks = ClassExams.TotalMarks; db.Entry(Exams).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(ClassExams)); }
public ClassSubject Update(ClassSubjectVM entity) { var instuteID = Convert.ToInt32(Membership.GetUser().PasswordQuestion); var classsubject = db.ClassSubject.Find(entity.Id); classsubject.SubjectNames = entity.SubjectNames; db.Entry(classsubject).Property(x => x.SubjectNames).IsModified = true; using (var Transaction = db.Database.BeginTransaction()) { try { db.SaveChanges(); db.Database.ExecuteSqlCommand("DELETE FROM SubjectBook WHERE ClassSubjectID = " + classsubject.Id); if (entity.SubjectBooks != null) { foreach (var item in entity.SubjectBooks) { if (!string.IsNullOrWhiteSpace(item.Name)) { item.ClassSubjectID = classsubject.Id; db.SubjectBook.Add(item); } } db.SaveChanges(); } db.SaveChanges(); Transaction.Commit(); } catch (Exception ex) { Transaction.Rollback(); } } return(classsubject); }
public void Update(ClassExams entity) { db.ClassExams.Attach(entity); db.Entry(entity).Property(x => x.TotalMarks).IsModified = true; db.SaveChanges(); }