public bool Insert(UserRoleModels model, ref string msg) { bool result = true; using (NuWebContext cxt = new NuWebContext()) { try { G_UserRole item = new G_UserRole(); item.Id = Guid.NewGuid().ToString(); item.RoleID = model.RoleID; item.EmployeeID = model.EmployeeID; item.IsActive = model.IsActive; //====== item.CreatedDate = DateTime.Now; item.CreatedUser = model.CreatedUser; item.ModifiedDate = DateTime.Now; item.ModifiedUser = model.ModifiedUser; cxt.G_UserRole.Add(item); cxt.SaveChanges(); } catch (Exception ex) { _logger.Error(ex); result = false; } finally { if (cxt != null) { cxt.Dispose(); } } } return(result); }
public bool InsertOrUpdate(List <UserRoleModels> models, ref string msg) { bool result = true; using (NuWebContext cxt = new NuWebContext()) { try { List <G_UserRole> listInsert = new List <G_UserRole>(); List <G_UserRole> listUpdate = new List <G_UserRole>(); List <G_UserRole> listDelete = new List <G_UserRole>(); G_UserRole item = null; foreach (var model in models) { var itemExsit = cxt.G_UserRole.Where(x => x.Id.Equals(model.Id)).FirstOrDefault(); if (itemExsit == null) //Insert { if (!model.IsActive) { continue; } item = new G_UserRole(); item.Id = Guid.NewGuid().ToString(); item.RoleID = model.RoleID; item.EmployeeID = model.EmployeeID; item.IsActive = model.IsActive; //====== item.CreatedDate = model.CreatedDate; item.CreatedUser = model.CreatedUser; item.ModifiedDate = model.ModifiedDate; item.ModifiedUser = model.ModifiedUser; listInsert.Add(item); } else { //Delete if (!model.IsActive) { listDelete.Add(itemExsit); } else //Update { item = itemExsit; item.Id = itemExsit.Id; item.ModifiedUser = model.ModifiedUser; item.ModifiedDate = model.ModifiedDate; listUpdate.Add(item); } } } //Insert if (listInsert.Count > 0) { cxt.G_UserRole.AddRange(listInsert); cxt.SaveChanges(); } //Update if (listUpdate.Count > 0) { cxt.SaveChanges(); } //Delete if (listDelete.Count > 0) { cxt.G_UserRole.RemoveRange(listDelete); cxt.SaveChanges(); } } catch (Exception ex) { _logger.Error(ex); result = false; } finally { if (cxt != null) { cxt.Dispose(); } } } return(result); }