public TrainingSession AddTrainingSession(int siteId, int trainingId, int employeeId, DateTime start, int duration, bool delivered) { // Because we want to have access to the employee in position property on our training session, we need some way to load this from the database. // One way would be using lazy loading, so that when we request this property it is loaded from the database. However, lazy loading is not enabled. // So, what we need to do is make use of the EF Context caching. When we load something from the database, context stores this in a cache. It is then // loaded for any entity that references this loaded data. In this case, our training session references the EmployeeInPosition that we are loading below // As a result, when we save the training session to the database, context will set the EmployeeInPosition property of the training session for us, using // the cache ISpecification <EmployeeInPosition> specification = new Specification <EmployeeInPosition>(e => e.Id == employeeId); specification.FetchStrategy = specification.FetchStrategy.Include(e => e.Employee); var employeeInTrainingPosition = _repoEmployeeInTrainingPos.Find(specification); var trainingsession = new TrainingSession { SiteId = siteId, TrainingId = trainingId, EmployeeTrainerId = employeeId, Start = start, DurationInMinutes = duration, Delivered = delivered }; _repoTrainingSession.Add(trainingsession); return(trainingsession); }
public TrainingSession AddTrainingSession(int siteId, int trainingId, int employeeTrainerId, DateTime start, int duration, bool deliveried) { var site = _repoSite.Get(siteId); var training = _repoTraining.Get(trainingId); var employeeTrainer = _repoEmployeeInPosition.Get(employeeTrainerId); var trainingsession = new TrainingSession { Site = site, Training = training, EmployeeInTrainingPostion = (employeeTrainer as EmployeeInTrainingPosition), Start = start, DurationInMinutes = duration, Delivered = deliveried }; _repoTrainingSession.Add(trainingsession); return(trainingsession); }