public void EditAsync_ReturnsCorrectNumberOfModifiedEntries() { var options = new DbContextOptionsBuilder <ApplicationDbContext>().UseInMemoryDatabase(Guid.NewGuid().ToString()).Options; using (var dbContext = new ApplicationDbContext(options)) { JobPosition jobPosition = new JobPosition() { Name = "newJobPos", }; dbContext.JobPositions.Add(jobPosition); dbContext.SaveChanges(); OperatingLocation operatingLocation = new OperatingLocation() { Town = "Sofia", Address = "test street", ImageUrl = "kgkkkgk", }; dbContext.OperatingLocations.Add(operatingLocation); dbContext.SaveChanges(); Employee employee = new Employee() { FirstName = "Ivan", MiddleName = "Ivanov", LastName = "Ivanov", PhoneNumber = "0897924218", Email = "*****@*****.**", Town = "Sofia", Address = "address 1", ImageUrl = "aasdfag", OperatingLocationId = operatingLocation.Id, JobPositionId = jobPosition.Id, }; var employeesService = new EmployeesService(dbContext); dbContext.Employees.Add(employee); dbContext.SaveChanges(); var employeeObj = dbContext.Employees.FirstOrDefaultAsync(); EditEmployeeServiceModel model = new EditEmployeeServiceModel { Id = employeeObj.Result.Id, FirstName = "new name", }; var result = employeesService.EditAsync(model); Assert.Equal(1, result.Result); } }
public async Task EditAsyncShouldWorkCorrectly() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; var dbContext = new ApplicationDbContext(options); var userRepo = new EfDeletableEntityRepository <ApplicationUser>(dbContext); var service = new EmployeesService(userRepo); await userRepo.AddAsync(new ApplicationUser { FirstName = "ivan", LastName = "ivanov", MiddleName = "ivanov", EGN = "1234567890", }); await userRepo.SaveChangesAsync(); var customerId = (await userRepo.AllAsNoTracking() .FirstOrDefaultAsync()).Id; var customer = (await service.GetByIdAsync <EmployeeModel>(customerId)).To <EmployeeModel>(); var newName = "Pesho"; var newEGN = "0987654321"; customer.FirstName = newName; customer.EGN = newEGN; await service.EditAsync <EmployeeModel>(customer); customer = (await service.GetByIdAsync <EmployeeModel>(customerId)).To <EmployeeModel>(); Assert.Equal(newName, customer.FirstName); Assert.Equal(newEGN, customer.EGN); }
/// <summary> /// 修改 /// </summary> /// <param name="id">id</param> /// <param name="password">密码</param> /// <param name="uName">名称</param> /// <param name="sex">性别</param> /// <param name="age">年龄</param> /// <param name="phone">手机号</param> /// <param name="email">邮箱</param> /// <param name="address">地址</param> /// <param name="image">头像</param> /// <param name="remarks">备注</param> /// <param name="status">状态</param> /// <param name="branchId">部门</param> /// <returns></returns> public async Task EditEmp(Guid id, string password, string uName, bool sex, int age, string phone, string email, string address, string image, string remarks, bool status, Guid branchId) { using IEmployeesService employeesService = new EmployeesService(); if (await employeesService.GetAllAsync().AnyAsync(i => i.Id == id)) { var emp = employeesService.GetAllAsync().FirstOrDefault(i => i.Id == id); if (emp != null) { emp.Password = password; emp.Name = uName; emp.Sex = sex; emp.Age = age; emp.Phone = phone; emp.Email = email; emp.Address = address; emp.Image = image; emp.Remarks = remarks; emp.Status = status; emp.BranchId = branchId; } await employeesService.EditAsync(emp); } }