public void HireEmployee(NewEmployeeProfile profile)
 {
     using (var context = new NorthwindContext())
     {
         if (profile.RawPhoto != null)
         {
             profile.RawPhoto = OleImageHelper.AddOleHeader(profile.RawPhoto);
         }
         var employee = new Employee()
         {
             FirstName       = profile.FirstName,
             LastName        = profile.LastName,
             Title           = profile.Title,
             TitleOfCourtesy = profile.TitleOfCourtesy,
             BirthDate       = profile.BirthDate,
             HireDate        = profile.StartDate,
             Address         = profile.Address,
             City            = profile.City,
             Region          = profile.Region,
             PostalCode      = profile.PostalCode,
             Country         = profile.Country,
             HomePhone       = profile.HomePhone,
             Extension       = profile.Extension,
             Photo           = profile.RawPhoto,
             Notes           = profile.Notes,
             ReportsTo       = profile.Supervisor
         };
         context.Employees.Add(employee);
         context.SaveChanges();
     }
 }
Esempio n. 2
0
    protected void AddStaff_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            DateTime parsedDate;
            int      parsedId;
            var      hire = new NewEmployeeProfile();
            hire.Title = JobTitle.Text;
            hire.Notes = Notes.Text;
            if (DateTime.TryParse(HireDate.Text, out parsedDate))
            {
                hire.StartDate = parsedDate;
            }
            if (int.TryParse(SupervisorList.SelectedValue, out parsedId))
            {
                hire.Supervisor = parsedId;
            }
            hire.Extension       = Extension.Text;
            hire.FirstName       = FirstName.Text;
            hire.LastName        = LastName.Text;
            hire.TitleOfCourtesy = CourtesyTitle.Text;
            if (DateTime.TryParse(BirthDate.Text, out parsedDate))
            {
                hire.BirthDate = parsedDate;
            }
            if (Photo.HasFile)
            {
                hire.RawPhoto = Photo.FileBytes;
            }
            hire.Address    = Address.Text;
            hire.City       = City.Text;
            hire.Region     = Region.Text;
            hire.Country    = Country.Text;
            hire.PostalCode = PostalCode.Text;
            hire.HomePhone  = Phone.Text;

            var controller = new HumanResourcesController();
            try
            {
                controller.HireEmployee(hire);
                MessageLabel.Text    = "Employee Added";
                MessagePanel.Visible = true;
            }
            catch (Exception ex)
            {
                MessageLabel.Text    = ex.Message;
                MessagePanel.Visible = true;
            }
        }
    }