public static async Task<Employee> AddEmployee(Employee employee)
 {
     using (var context = new DbModel<Employee>())
     {
         if (employee.Dependents == null)
             employee.Dependents = new List<Dependent>();
         return await context.Add(employee);
     }
 }
Exemple #2
0
 /// <summary>
 /// Add Demo data to the database (10 random employee names will be added)
 /// </summary>
 /// <param name="count">The count of employees to add (default = 10)</param>
 /// <returns>Returns a list of names for those employees added</returns>
 public async Task <IHttpActionResult> Get(int count = 10)
 {
     try
     {
         var employees = new List <string>();
         using (var context = new DbModel <Employee>())
         {
             for (int i = 0; i < count; i++)
             {
                 string name = RandomName;
                 int    age  = rnd.Next(28, 45);
                 employees.Add(name);
                 var newEmployee = new Employee
                 {
                     Name             = name,
                     Email            = name.Replace(' ', '.') + "@test.us",
                     Age              = age,
                     PaycheckAmount   = 2000.00,
                     PaychecksPerYear = 26,
                     HireDate         = DateTime.Now.AddDays(-rnd.Next(500, (age - 22) * 300)),
                     Dependents       = new List <Dependent>()
                 };
                 // Add some dependents
                 for (int j = 0; j < rnd.Next(0, 4); j++)
                 {
                     var dependent = new Dependent
                     {
                         Name = RandomName,
                         Age  = rnd.Next(age++, age + 10)
                     };
                     newEmployee.Dependents.Add(dependent);
                 }
                 await context.Add(newEmployee);
             }
         }
         return(Ok(employees));
     }
     catch (Exception e)
     {
         logger.Error(e);
         return(InternalServerError(e));
     }
 }
 /// <summary>
 /// Add Demo data to the database (10 random employee names will be added)
 /// </summary>
 /// <param name="count">The count of employees to add (default = 10)</param>
 /// <returns>Returns a list of names for those employees added</returns>        
 public async Task<IHttpActionResult> Get(int count = 10)
 {
     try
     {
         var employees = new List<string>();
         using (var context = new DbModel<Employee>())
         {
             for (int i = 0; i < count; i++)
             {
                 string name = RandomName;
                 int age = rnd.Next(28, 45);
                 employees.Add(name);
                 var newEmployee = new Employee
                 {
                     Name = name,
                     Email = name.Replace(' ', '.') + "@test.us",
                     Age = age,
                     PaycheckAmount = 2000.00,
                     PaychecksPerYear = 26,
                     HireDate = DateTime.Now.AddDays(-rnd.Next(500, (age - 22) * 300)),
                     Dependents = new List<Dependent>()
                 };
                 // Add some dependents
                 for (int j = 0; j < rnd.Next(0, 4); j++)
                 {
                     var dependent = new Dependent
                     {
                         Name = RandomName,
                         Age = rnd.Next(age++, age + 10)
                     };
                     newEmployee.Dependents.Add(dependent);
                 }
                 await context.Add(newEmployee);
             }
         }
         return Ok(employees);
     }
     catch (Exception e)
     {
         logger.Error(e);
         return InternalServerError(e);
     }
 }