public ActionResult Create([Bind(Include = "Id,Name,Description,ActiveStatus")] Role role) { if (ModelState.IsValid) { role.Id = Guid.NewGuid(); db.Roles.Add(role); db.SaveChanges(); using (var insertDb = new HRMContext()) { RolePermission rolePermission = new RolePermission(); insertDb.RolePermissions.RemoveRange(db.RolePermissions.Where(r => r.RoleId == role.Id)); db.ModulePermissions.ToList().ForEach(mp => { rolePermission = new RolePermission() { Id = Guid.NewGuid(), RoleId = role.Id, ModuleId = mp.ModuleId, PermissionName = mp.PermissionName, Permission = false }; insertDb.RolePermissions.Add(rolePermission); }); insertDb.SaveChanges(); } return(RedirectToAction("Index")); } return(View(role)); }
public IActionResult Create(Dept dept) { if (ModelState.IsValid) { _context.Depts.Add(dept); _context.SaveChanges(); return(RedirectToAction("Index")); } return(View(dept)); }
public ActionResult Create([Bind(Include = "Id,loginId,Password,ActiveStatus")] User user) { if (ModelState.IsValid) { user.Id = Guid.NewGuid(); db.Users.Add(user); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(user)); }
public static void WriteErrorLog(ErrorLog errorLog) { try { if (errorLog.ErrorMessage.Length > 1023) { errorLog.ErrorMessage = errorLog.ErrorMessage.Substring(0, 1000); } using (HRMContext db = new HRMContext()) { errorLog.Id = Guid.NewGuid(); errorLog.CreateDate = DateTime.UtcNow; db.ErrorLogs.Add(errorLog); db.SaveChanges(); } } catch (Exception ex) { using (var db = new HRMContext()) { var log = new ErrorLog(); log.Id = Guid.NewGuid(); log.CreateDate = DateTime.UtcNow; log.ErrorFor = "Exception to write error log"; log.ErrorFrom = "WriteErrorLog"; log.ErrorMessage = ex.Message.Length > 1023 ? ex.Message.Substring(0, 1000) : ex.Message; db.ErrorLogs.Add(log); db.SaveChanges(); } } }
public ActionResult Create([Bind(Include = "Id,EmpCode,FullName,NickName,DesignationId,DeptId,Phone,Email,Address")] Employee employee) { if (ModelState.IsValid) { employee.ActionDate = DateTime.Now; if (employee.Id != 0) { db.Entry(employee).State = EntityState.Modified; } else { db.Employees.Add(employee); } db.SaveChanges(); return(RedirectToAction("Create")); } ViewBag.DeptId = new SelectList(db.Depts, "Id", "Name", employee.DeptId); ViewBag.DesignationId = new SelectList(db.Designations, "Id", "Name", employee.DesignationId); return(View(employee)); }
public ActionResult Create(Employee employee) { if (ModelState.IsValid) { employee.ActionDate = DateTime.Now; if (employee.Id != 0) { db.Entry(employee).State = EntityState.Modified; } else { db.Employees.Add(employee); } db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.DeptId = new SelectList(db.Depts, "DeptId", "Name", employee.DeptId); ViewBag.DesignationId = new SelectList(db.Designations, "Id", "Name", employee.DesignationId); return(View(employee)); }
// PUT: odata/Employee(5) public IHttpActionResult Put([FromODataUri] int key, Delta <Employee> patch) { Validate(patch.GetEntity()); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Employee employee = db.Employees.Find(key); if (employee == null) { return(NotFound()); } patch.Put(employee); try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!EmployeeExists(key)) { return(NotFound()); } else { throw; } } return(Updated(employee)); }
public static void Initialize(IServiceProvider serviceProvider) { using (var context = new HRMContext( serviceProvider.GetRequiredService <DbContextOptions <HRMContext> >())) { // Check any employee exists if (context.Employees.Any()) { return; // Data already exists no need to generate } context.Employees.AddRange( new Employee { Name = "Md. Mahedee Hasan", Designation = "Head of Software Development", FathersName = "Yeasin Bhuiyan", MothersName = "Moriom Begum", DateOfBirth = new DateTime(1984, 12, 19, 00, 00, 00) }, new Employee { Name = "Khaleda Islam", Designation = "Software Engineer", FathersName = "Shahidul Islam", MothersName = "Momtaz Begum", DateOfBirth = new DateTime(1990, 10, 29, 00, 00, 00) }, new Employee { Name = "Tahiya Hasan Arisha", Designation = "Jr. Software Engineer", FathersName = "Md. Mahedee Hasan", MothersName = "Khaleda Islam", DateOfBirth = new DateTime(2017, 09, 17, 00, 00, 00) } ); context.SaveChanges(); } }
public int SaveChanges() { return(DbContext.SaveChanges()); }
public bool Commit() { return(_context.SaveChanges() > 0); }