コード例 #1
0
 public Models.Employee MapFromDAL(WebAPI_DB.Employee emp)
 {
     return(new Models.Employee()
     {
         Id = emp.Id,
         Name = emp.Name,
         JoiningDate = emp.JoiningDate,
         Age = emp.Age
     });
 }
コード例 #2
0
        public WebAPI_DB.Employee MapToDAL(Models.Employee emp, WebAPI_DB.Employee empDAL)
        {
            if (empDAL == null)
            {
                empDAL = new WebAPI_DB.Employee();
            }

            empDAL.Id          = emp.Id;
            empDAL.Name        = emp.Name;
            empDAL.JoiningDate = emp.JoiningDate;
            empDAL.Age         = emp.Age;

            return(empDAL);
        }
コード例 #3
0
        public async Task <IHttpActionResult> PostEmployee([FromBody] Models.Employee emp)
        {
            //string s = employee.Name;
            //return Ok(employee);

            using (var context = new WebAPIDemoDBEntities())
            {
                WebAPI_DB.Employee employee = null;
                if (emp.Id == 0) //Add employee
                {
                    employee = new WebAPI_DB.Employee();
                    employee = MapToDAL(emp, employee);
                    //employee.Id = DBNull.Value;

                    context.Employees.Add(employee);
                }
                else
                {
                    employee = context.Employees.ToList().FirstOrDefault((p) => p.Id == emp.Id);
                    employee = MapToDAL(emp, employee);
                }

                await context.SaveChangesAsync();

                //var employee = context.Employees.ToList().FirstOrDefault((p) => p.Id == id);
                //if (employee == null)
                //{
                //    return NotFound();
                //}
                return(Ok(employee));
            }

            //if (!ModelState.IsValid)
            //{
            //    return BadRequest(ModelState);
            //}
            //_context.employee.Add(employee);
            //await _context.SaveChangesAsync();
            //return CreatedAtAction("GetEmployee", new
            //{
            //    id = employee.ID
            //}, employee);
        }