public TaskResult <bool> SaveStory(StoryDetail story) { TaskResult <bool> result = null; try { if (story.Id == null)//new { Story storyEntity = new Story { Content = story.Content, DateCreated = DateTime.Now, Description = story.Description, Title = story.Title, UserId = story.UserId.Value }; _storyRepository.Insert(storyEntity); //_storyRepository.SaveChanges(); _storyRepository.AddStoryToGroups(storyEntity, story.GroupDetails.Select(a => a.Id.Value).AsEnumerable()); _storyRepository.SaveChanges(); } else { Story _story = _storyRepository.GetSingle(a => a.Id == story.Id); _story.Description = story.Description; _story.Title = story.Title; _story.Content = story.Content; _story.DateModified = DateTime.Now; IEnumerable <Group> groups = _story.Groups.ToList(); foreach (var gr in groups) { _story.Groups.Remove(gr); } _storyRepository.Update(_story); _storyRepository.SaveChanges(); _storyRepository.AddStoryToGroups(_story, story.GroupDetails.Select(a => a.Id.Value).AsEnumerable()); _storyRepository.SaveChanges(); } result = new TaskResult <bool> { Data = true, state = StatusState.DoneState }; } catch (Exception e) { result = new TaskResult <bool> { Data = false, state = StatusState.CancelState }; } return(result); }