/// <summary> /// This method tries to update an entity in the database through setting EntityFramework Core's Entry property to EntityState.Modified. If the update fails an exception is thrown. If the update succeeds then the employee parameter object passed in is saved to the database. /// </summary> /// <param name="employee"></param> /// <param name="id"></param> /// <returns></returns> public async Task <bool> ModifyStateAsync(Domain.Models.Employee employee, int id) { var mappedEmployee = Mapper.MapEmployee(employee); /*_context.Entry(employee).State = EntityState.Modified;*/ _context.Entry(mappedEmployee).State = EntityState.Modified; try { await SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!EmployeeExists(id)) { return(false); // employee not found } else { throw; } } return(true); // it worked, so return true }
public async Task <bool> Do(int EmployeeId) { Domain.Models.Employee employee = await _ctx.Employees .Include(x => x.User) .Include(x => x.Admin) .Include(x => x.Moderator) .Where(x => x.Id == EmployeeId) .FirstOrDefaultAsync(); switch (employee.Role) { case "User": if (employee.User.IsActive) { employee.User.IsActive = false; } else { employee.User.IsActive = true; } break; case "Moderator": if (employee.Moderator.IsActive) { employee.Moderator.IsActive = false; } else { employee.Moderator.IsActive = true; } break; case "Admin": if (employee.Admin.IsActive) { employee.Admin.IsActive = false; } else { employee.Admin.IsActive = true; } break; case "None": break; } await _ctx.SaveChangesAsync(); return(true); }
public static Models.Employee MapEmployee(Domain.Models.Employee employee) { return(new Models.Employee { EmpId = employee.EmpId, FirstName = employee.FirstName, LastName = employee.LastName, Invoice = employee.Invoice.Select(MapInvoice).ToList(), Policies = employee.Policies.Select(MapPolicies).ToList() }); }
/// <summary> /// Wraps a call to EntityFramework Core Add. The call is made with a mapped entity (DataAccess.Models.Employee) instead of the domain model passed as a parameter. The DataAccess.Model is used to communicate with EF Core. /// /// EF Core Add: /// Finds an entity with the given primary key values. If an entity with the given primary key values is being tracked by the context, then it is returned immediately without making a request to the database. Otherwise, a query is made to the database for an entity with the given primary key values and this entity, if found, is attached to the context and returned. If no entity is found, then null is returned. /// </summary> /// <param name="entity"></param> /// <returns>Domain.Models.Employee</returns> public void Add(Domain.Models.Employee entity) { var mappedEntity = Mapper.MapEmployee(entity); _context.Set <Employee>().Add(mappedEntity); }