public void Speaker_should_map_to_SpeakerForm_with_valid_data() { var speaker = new Speaker { Email = "[email protected]", Languages = new [] {"JavaScript", "C#"}, Name = "Brandon Satrom", SpeakerId = 2343, State = "TX" }; var session = new Session { Speaker = speaker, Date = DateTime.Now, Id = 123, Location = "Here", Name = "Automapper for Noobs", Ratings = new List<int> { 3, 5, 2, 1, 3 } }; var sessions = new List<Session> {session, session}; speaker.Sessions = sessions; Mapper.CreateMap<Session, string>().ConvertUsing(s => s.Name); Mapper.CreateMap<Speaker, SpeakerForm>(); SpeakerForm speakerForm = Mapper.Map<Speaker, SpeakerForm>(speaker); Assert.IsNotNull(speakerForm); Assert.AreEqual("Brandon Satrom", speakerForm.Name); Assert.AreEqual(2, speakerForm.Sessions.Count()); }
public void InsertOrUpdate(Session session) { if (session.Id == default(int)) { // New entity context.Sessions.Add(session); } else { // Existing entity context.Sessions.Attach(session); context.Entry(session).State = EntityState.Modified; } }
public ActionResult Create(Session session) { if (ModelState.IsValid) { sessionRepository.InsertOrUpdate(session); sessionRepository.Save(); return RedirectToAction("Index"); } else { ViewBag.PossibleTags = tagRepository.All; ViewBag.PossibleSpeakers = speakerRepository.All; return View(); } }
public void InsertOrUpdate(Session session) { if (session.Id == default(int)) { session.Id = new Random().Next(1, 5000); _sessions.Add(session); } else { var savedSession = _sessions.Single(s => s.Id == session.Id); savedSession = session; } }