//public void  Delete(AddEmployee criteria)
        //{
        //    var deleted =  _ctx.Employees
        //                        .Include(e => e.Dependents)
        //                        .First(e => e.Id == criteria.Id);
        //    _ctx.Remove(deleted);
        //    _ctx.SaveChangesAsync();
        //}

        public int Add(AddEmployee emp)
        {
            try
            {
                _logger.LogInformation("Add new Employee");
                var result = new Entities.Employee
                {
                    FirstName  = emp.Employee.FirstName,
                    LastName   = emp.Employee.LastName,
                    Dependents = emp.Dependents.Select(ed => new Entities.Info
                    {
                        FirstName = ed.FirstName,
                        LastName  = ed.LastName
                    }).ToList()
                };
                _ctx.Add(result);
                _ctx.SaveChanges();
                return(result.Id);
            }
            catch (Exception ex)
            {
                _logger.LogError($"Failed to add employee: {ex}");
                return(0);
            }
        }
Example #2
0
        public async Task <int> Add(CreateEmployee command)
        {
            var newEmployee = new Entities.Employee
            {
                FirstName  = command.Employee.FirstName,
                LastName   = command.Employee.LastName,
                Dependents = command.Dependents.Select(d => new Entities.Person
                {
                    FirstName = d.FirstName,
                    LastName  = d.LastName
                }).ToList()
            };

            db.Add(newEmployee);
            await db.SaveChangesAsync();

            return(newEmployee.Id);
        }
 public void Save(Employee employee)
 {
     _employeeContext.Add(employee);
     _employeeContext.SaveChanges();
 }