public void Add(Member member) { var sportsIds = new HashSet <int>(member.MemberSports.Select(ms => ms.SportId)); var validSportIds = _context.Sports.Where(s => sportsIds.Contains(s.Id)) .Select(s => s.Id); member.MemberSports = validSportIds.Select(vid => new MemberSport { SportId = vid }).ToList(); _context.Add(member); _context.SaveChanges(); }
public void ShouldGetAllMembers() { var members = new List <Member> { new Member { Id = 1, FirstName = "Ronaldinho", LastName = "Gaucho", MemberSports = new List <MemberSport> { new MemberSport { MemberId = 1, SportId = 1 } } }, new Member { Id = 2, FirstName = "Ronaldinho", LastName = "Gaucho", MemberSports = new List <MemberSport> { new MemberSport { MemberId = 1, SportId = 1 } } } }; var options = new DbContextOptionsBuilder <ASMembershipContext>() .UseInMemoryDatabase("ShouldGetAllMembers") .Options; using (var context = new ASMembershipContext(options)) { context.Sports.AddRange(GetTestSports()); context.SaveChanges(); var memberRepository = new MemberRepository(context); foreach (var member in members) { memberRepository.Add(member); } } using (var context = new ASMembershipContext(options)) { var memberRepository = new MemberRepository(context); var result = memberRepository.GetAll(); Assert.Equal(2, result.Count); } }
public void ShouldUpdateMember() { var member = new Member { Id = 1, FirstName = "Ronaldinho", LastName = "Gaucho", MemberSports = new List <MemberSport> { new MemberSport { MemberId = 1, SportId = 1 } } }; var options = new DbContextOptionsBuilder <ASMembershipContext>() .UseInMemoryDatabase("ShouldUpdateMember") .Options; using (var context = new ASMembershipContext(options)) { context.Sports.AddRange(GetTestSports()); context.SaveChanges(); var memberRepository = new MemberRepository(context); memberRepository.Add(member); } using (var context = new ASMembershipContext(options)) { var memberRepository = new MemberRepository(context); member.FirstName = "Mo"; member.LastName = "Salah"; member.MemberSports.First().SportId = 2; memberRepository.Update(member); var result = memberRepository.GetById(member.Id); Assert.Equal("Mo", result.FirstName); Assert.Equal("Salah", result.LastName); Assert.Equal("Tennis", result.MemberSports.First().Sport.Name); } }
public void ShouldGetAllSports() { var options = new DbContextOptionsBuilder <ASMembershipContext>() .UseInMemoryDatabase("ShouldGetAllSports") .Options; var sports = GetTestSports(); using (var context = new ASMembershipContext(options)) { context.Sports.AddRange(sports); context.SaveChanges(); } using (var context = new ASMembershipContext(options)) { var sportRepository = new SportRepository(context); var result = sportRepository.GetAll(); Assert.Equal(sports.Count, result.Count); } }