public void AddEmployee(EmployeeDto dto) { var employee = Mapper.Map <Employee>(dto); context.Employees.Add(employee); context.SaveChanges(); }
public void AddEmployee(EmployeeDto employeeDto) { Employee employee = Mapper.Map <EmployeeDto, Employee>(employeeDto); context.Employees.Add(employee); context.SaveChanges(); }
public IHttpActionResult PutEmployee(int id, Employee employee) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != employee.Id) { return(BadRequest()); } db.Entry(employee).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!EmployeeExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
static string Task2() { _context.Towns.Add(new Towns { Name = "Lalaland" }); _context.Addresses.Add(new Addresses { AddressText = "18 Solomatina", TownId = _context.Towns .AsEnumerable().LastOrDefault().TownId }); _context.SaveChanges(); var brownEmployees = _context.Employees .Select(e => e) .Where(e => e.LastName == "Brown").ToArray(); var sb = new StringBuilder(); foreach (var e in brownEmployees) { e.AddressId = _context.Addresses.AsEnumerable().LastOrDefault().AddressId; sb.AppendLine($"{e.FirstName} {e.LastName} {e.MiddleName} {e.JobTitle} {e.Salary} {e.HireDate} {e.AddressId}"); } return(sb.ToString().TrimEnd()); }
public ActionResult AddorEdit(Employee emp) { try { if (emp.ImageUpload != null) { string fileName = Path.GetFileNameWithoutExtension(emp.ImageUpload.FileName); string extension = Path.GetExtension(emp.ImageUpload.FileName); fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension; emp.ImagePath = "~/AppFiles/Images/" + fileName; emp.ImageUpload.SaveAs(Path.Combine(Server.MapPath("~/AppFiles/Images/"), fileName)); } using (EmployeesContext db = new EmployeesContext()) { if (emp.EmployeeID == 0) { db.Employees.Add(emp); db.SaveChanges(); } else { db.Entry(emp).State = EntityState.Modified; db.SaveChanges(); } } return(Json(new { success = true, html = GlobalClass.RenderRazorViewToString(this, "ViewAll", GetAllEmployee()), message = "Submitted Successfully" }, JsonRequestBehavior.AllowGet)); } catch (Exception ex) { return(Json(new { success = true, message = ex.Message }, JsonRequestBehavior.AllowGet)); } }
//Task 2 static string AddNewAdressForBrown() { var town = new Towns() { Name = "Moscow" }; _context.Towns.Add(town); _context.SaveChanges(); var address = new Addresses() { AddressText = "Brown street", Town = town }; _context.Addresses.Add(address); _context.SaveChanges(); var browns = ( from employee in _context.Employees where employee.LastName == "Brown" select employee ).ToList(); browns.ForEach(e => e.Address = address); var sb = new StringBuilder(); foreach (var b in browns) { sb.AppendLine($"{b.FirstName} {b.LastName} {b.MiddleName} {b.JobTitle} {b.DepartmentId} {b.ManagerId} {b.Address.AddressText}"); } return(sb.ToString().TrimEnd()); }
//insert data, will be hit by Signup Page public Boolean insertData(CustomEmployee employeeDetails) { try { EmployeeDetails employee = new EmployeeDetails() { EmpName = employeeDetails.EmpName, Username = employeeDetails.Username, EmpPhone = employeeDetails.EmpPhone, EmpAddress = employeeDetails.EmpAddress, EmpRole = employeeDetails.EmpRole, EmpPassword = employeeDetails.EmpPassword, EmpProjectId = employeeDetails.EmpProjectId, AdminFlag = employeeDetails.AdminFlag, EmpFlag = employeeDetails.EmpFlag }; _employeesContext.EmployeeDetails.Add(employee); _employeesContext.SaveChanges(); return(true); } catch (SqlException sqle) { return(false); } }
public void SetAddress(int employeeId, string address) { Employee employee = this.GetEmploye <Employee>(employeeId); employee.Address = address; context.SaveChanges(); }
public Employee CreateEmployee(Employee employee) { db.Employees.Add(employee); db.SaveChanges(); // Now we generate a Base36 string for the DB id: db.SaveChanges(); return(employee); }
public void SetManager(int employeeId, int managerId) { var employee = context.Employees.Find(employeeId); var manager = context.Employees.Find(managerId); employee.ManagerId = managerId; manager.Employees.Add(employee); context.SaveChanges(); }
public void Add(string firstName, string lastName, decimal salary) { var employeeDto = new EmployeeDto { FirstName = firstName, LastName = lastName, Salary = salary }; var employee = Mapper.Map <Employee>(employeeDto); context.Employees.Add(employee); context.SaveChanges(); }
public string AddEmployee(EmployeeDto dto) { var employee = Mapper.Map <Employee>(dto); context.Employees.Add(employee); context.SaveChanges(); return("Added employee"); }
public ActionResult Create([Bind(Include = "Id,Name,Department")] Employee employee) { if (ModelState.IsValid) { db.Employees.Add(employee); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(employee)); }
public ActionResult Create(Employees employees) { if (ModelState.IsValid) { db.Employees.Add(employees); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(employees)); }
public ActionResult Create([Bind(Include = "ID,Name,Salary")] Employees employees) { if (ModelState.IsValid) { db.Employees.Add(employees); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(employees)); }
public ActionResult Create([Bind(Include = "Id,DepartmentName,Director")] Department department) { if (ModelState.IsValid) { db.Departments.Add(department); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(department)); }
public void Create(string firstName, string lastName, decimal salary) { var employee = new Employee() { FirstName = firstName, LastName = lastName, Salary = salary }; context.Employees.Add(employee); context.SaveChanges(); }
public ActionResult Create([Bind(Include = "Id,Fullname,CIN,DepartmentId")] Employee employee) { if (ModelState.IsValid) { db.Employees.Add(employee); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.DepartmentId = new SelectList(db.Departments, "Id", "DepartmentName", employee.DepartmentId); return(View(employee)); }
public ActionResult Create(Employee employee) { try { dbEmployee.Employees.Add(employee); dbEmployee.SaveChanges(); return RedirectToAction("Index"); } catch { return View(); } }
public void AddEmployee(EmployeeDto employeeDto) { bool hasEmployee = db.Employees.Any(e => e.FirstName == employeeDto.FirstName && e.LastName == employeeDto.LastName); if (hasEmployee) { throw new ArgumentException("There is already an employee with given names."); } var employee = Mapper.Map <Employee>(employeeDto); db.Employees.Add(employee); db.SaveChanges(); }
public void RepositoryDeleteMethod_MustDeleteEntry() { var testOrganization1 = new Organization { Name = "TestOrganization1" }; var testOrganization2 = new Organization { Name = "TestOrganization2" }; var testOrganization3 = new Organization { Name = "TestOrganization3" }; _context.Organizations.Add(testOrganization1); _context.Organizations.Add(testOrganization2); _context.Organizations.Add(testOrganization3); _context.SaveChanges(); _unitOfWork.Organizations.Delete(testOrganization3); _unitOfWork.Save(); var newContext = CreateInMemoryDbContext(); var deletedEntry = newContext.Organizations.FirstOrDefault(x => x.Name == testOrganization3.Name); Assert.AreEqual(newContext.Organizations.Count(), 2); Assert.AreSame(null, deletedEntry); }
/*Original code (end) -----------------------------------------------------*/ private static void TestEmployees() { // Employee DB created? (Library example method) using (var context = new EmployeesContext()) { // Creates the database if not exists var dbNotExisting = context.Database.EnsureCreated(); // dbNotExisting = true -> create DB, false -> does nothing // Add some data if the DB does not exist // (If you run the code below and the primary keys of these books are already declared in the DB it will throw an error) Console.WriteLine($"EmployeeDB do not exist = {dbNotExisting}"); if (dbNotExisting) { // Adds some employees context.Employees.Add(new Employee { Name = "Justin", Surname = "Time", Job = "Rellotger", Salary = 1000 }); context.Employees.Add(new Employee { Name = "Elena", Surname = "Nito Del Bosque", Job = "Jardiner", Salary = 5000 }); // Saves changes context.SaveChanges(); } } }
public bool Add(Employee obj) { try { obj.Id = Guid.NewGuid().ToString(); _dbContext.Employees.Add(obj); _dbContext.SaveChanges(); _logger.Info($"{obj} was successfully added."); return(true); } catch (Exception ex) { _logger.Fatal <Exception>($"Error adding: {obj}", ex); return(false); } }
public static void Initialize(EmployeesContext context) { context.Database.EnsureCreated(); if (context.EmployeeList.Any()) { return; } var employees = new Employee[] { new Employee { Name = "lala", Surname = "blabla" }, new Employee { Name = "lala", Surname = "blabla" }, new Employee { Name = "lala", Surname = "blabla" } }; foreach (Employee employee in employees) { context.EmployeeList.Add(employee); } context.SaveChanges(); }
private static void SeedDataBase(IEnumerable <Employee> employees) { using (EmployeesContext context = new EmployeesContext()) { context.Employees.AddRange(employees); context.SaveChanges(); } }
private static void SeedDatabase(IEnumerable <Employee> managers) { using (var context = new EmployeesContext()) { context.Employees.AddRange(managers); context.SaveChanges(); } }
public ActionResult newCabinAssignment(Employee e) { if (ModelState.IsValid) { using (EmployeesContext db = new EmployeesContext()){ var isQualified = ( from cc in db.allcrew from att in db.flight where cc.person == e.cabinCrewId && att.id == e.flightId && cc.forAircraftType == att.aircraft select new { cc.person }).Count(); if (isQualified > 0) { var isDuplicate = ( from ee in db.crew where ee.cabinCrewId == e.cabinCrewId && ee.flightId == e.flightId select new { ee.cabinCrewId }).Count(); if (isDuplicate == 0) { db.crew.Add(e); db.SaveChanges(); return(RedirectToAction("Index")); } else { ViewBag.Message = "Crew member already assigned to flight"; } } else { ViewBag.Message = "Crew member not qualified for the specified flight"; } } } var crew = GetAllCrew(); ViewBag.list = new SelectList(crew.ToList(), "id", "name"); var flightlist = GetAllFlights(); ViewBag.flist = new SelectList(flightlist.ToList(), "id", "flightmes"); Employee ex = new Employee(); return(View(ex)); }
static void AddAddress() { Addresses address = new Addresses { AddressText = " 221b Baker street", TownId = 23 }; _context.Addresses.Add(address); _context.SaveChanges(); var resEmployees = _context.Employees .Where(e => e.LastName.Contains("Brown")); var newAddress = _context.Addresses .Where(ad => ad.AddressText.Contains(address.AddressText)) .FirstOrDefault(); foreach (var e in resEmployees) { e.AddressId = newAddress.AddressId; } _context.SaveChanges(); var employees = _context.Employees .Where(e => e.LastName == "Brown") .Select(e => new { e.FirstName, e.LastName, e.MiddleName, e.JobTitle, e.HireDate, e.Salary, Department = e.Department.Name, Address = e.Address.AddressText, Manager = e.Department.Manager.FirstName + " " + e.Department.Manager.LastName }) .ToList(); foreach (var e in employees) { Console.WriteLine("{0} {1} {2} {3} {4} {5} {6} {7} {8}", e.FirstName, e.LastName, e.MiddleName, e.JobTitle, e.HireDate, e.Salary, e.Department, e.Address, e.Manager); } }
//Task 2 static string RelocateEmployees() { Addresses address = new Addresses { TownId = 11, AddressText = "137 Brown St." }; var adresses = _context.Addresses.Add(address); _context.SaveChanges(); var SelectAdressId = _context.Addresses .Select(a => new { a.AddressId, a.AddressText }) .Where(a => a.AddressText.Equals("137 Brown St.")) .FirstOrDefault(); var employees = _context.Employees .Include("Address") .Include("Department") .Where(e => e.LastName == "Brown"); foreach (var e in employees) { e.AddressId = SelectAdressId.AddressId; } _context.SaveChanges(); var sb = new StringBuilder(); foreach (var e in employees) { sb.AppendLine($"{e.FirstName} {e.LastName} {e.MiddleName}, {e.JobTitle}, {e.Salary}, {e.Address.AddressText}, department name: {e.Department.Name}, hire date: {e.HireDate};"); } return(sb.ToString().TrimEnd()); }
public ActionResult DeleteCrewFlight(int cId, int fId, int?jj) { using (EmployeesContext db = new EmployeesContext()) { Employee ex = db.crew.Find(cId, fId); db.Entry(ex).State = EntityState.Deleted; db.SaveChanges(); return(RedirectToAction("Index")); } }