public void RemoveTeam_Test()
        {
            //Arrange
            var options = new DbContextOptionsBuilder <TeamContext>().UseInMemoryDatabase($"TeamContext_{ Guid.NewGuid() }").Options;

            using (var context = new TeamContext(options))
            {
                context.Add(new Team()
                {
                    Id               = 1,
                    City             = "Chicago",
                    Name             = "Chicago Fire",
                    DateOfFoundation = DateTime.Now
                });

                context.Add(new Team()
                {
                    Id               = 2,
                    City             = "Los Angeles",
                    Name             = "Los Angeles Galaxy",
                    DateOfFoundation = DateTime.Now
                });

                context.SaveChanges();

                var repository = new TeamsRepository(context);

                //Act
                repository.RemoveTeam(1);
                var teams = repository.GetTeams().ToList();

                //Assert
                Assert.Equal(1, teams.Count);
            }
        }
Beispiel #2
0
        public static void Main(string[] args)
        {
            try
            {
                using (var teamCtx = new TeamContext())
                {
                    Person me = new Person()
                    {
                        Firstname = args.Length > 0 ? args[0] : "Chris",
                        Lastname  = args.Length > 1 ? args[1] : "White",
                        Age       = args.Length > 2 && !String.IsNullOrEmpty(args[2]) ? Convert.ToInt32(args[2]) : 32
                    };

                    var personInDb = teamCtx.Person.FirstOrDefault(T => T.Firstname == me.Firstname && T.Lastname == me.Lastname && T.Age == me.Age);

                    var  teamName = args.Length > 3 ? args[3] : "Team Ace";
                    Team ace      = new Team()
                    {
                        TeamName = teamName
                    };

                    var teamInDb = teamCtx.Team.FirstOrDefault(T => T.TeamName == teamName);
                    me  = personInDb ?? me;
                    ace = teamInDb ?? ace;

                    TeamAllocation teamAlloc = new TeamAllocation();
                    teamAlloc.Team   = ace;
                    teamAlloc.Person = me;

                    var teamAllocInDb = teamCtx.TeamAllocation.FirstOrDefault(T => T.Team == ace && T.Person == me);

                    // Determin if we need to update or insert
                    if (personInDb != null)
                    {
                        teamCtx.Attach(me);
                    }
                    else
                    {
                        teamCtx.Add(me);
                    }

                    // Determin if we need to update or insert
                    if (teamInDb != null)
                    {
                        teamCtx.Team.Attach(ace);
                    }
                    else
                    {
                        teamCtx.Team.Add(ace);
                    }

                    // Determin if we need to update or insert
                    if (teamAllocInDb != null)
                    {
                        teamCtx.TeamAllocation.Attach(teamAllocInDb);
                    }
                    else
                    {
                        teamCtx.TeamAllocation.Add(teamAlloc);
                    }

                    teamCtx.SaveChanges();

                    var teams = teamCtx.Team.Include(T => T.TeamAllocation).ThenInclude(T => T.Person).ToList();

                    foreach (var team in teams)
                    {
                        Console.WriteLine($"In team {team.TeamName}");
                        foreach (var person in team.TeamAllocation)
                        {
                            Console.WriteLine($"\t\t Id: {person.Person.PersonId}, {person.Person.Firstname} {person.Person.Lastname}, Aged {person.Person.Age}");
                        }
                    }
                    Console.WriteLine("");
                }
            }
            catch (Exception ex) {
                Console.WriteLine(ex.ToString());
            }
        }
Beispiel #3
0
 public void Add <T>(T entity) where T : class
 {
     _logger.LogInformation($"Adding an object of type {entity.GetType()} to the context.");
     _context.Add(entity);
 }