private static void AddUserGame(XElement xmlUser, DiabloEntities context)
        {
            var newUser = CreateUserToAdd(xmlUser);
            context.Users.Add(newUser);
            Console.WriteLine("Successfully added user {0}", newUser.Username);

            foreach (var xmlGame in xmlUser.Descendants("game"))
            {
                var gameName = xmlGame.Element("game-name").Value;
                var characterName = xmlGame.Element("character").Attribute("name").Value;

                var existingGame = context.Games.First(g => g.Name.Equals(gameName));
                var character = context.Characters.First(ch => ch.Name == characterName);

                var gameCash = decimal.Parse(xmlGame.Element("character").Attribute("cash").Value);
                var level = int.Parse(xmlGame.Element("character").Attribute("level").Value);
                var joinedOn = DateTime.Parse(xmlGame.Element("joined-on").Value);

                var userGame = new UsersGame()
                {
                    CharacterId = character.Id,
                    GameId = existingGame.Id,
                    Cash = gameCash,
                    JoinedOn = joinedOn,
                    Level = level,
                    User = newUser
                };

                context.UsersGames.Add(userGame);
                Console.WriteLine("User {0} successfully added to game {1}", newUser.Username, existingGame.Name);
            }
        }
        public static void Main()
        {
            var filePath = @"..\..\..\";
            var fileName = @"finished-games.xml";

            var context = new DiabloEntities();
            var finishedGames = context.Games
                .Where(g => g.IsFinished)
                .OrderBy(g => g.Name)
                .ThenBy(g => g.Duration)
                .Select(g => new
                {
                    Name = g.Name,
                    Duration = g.Duration,
                    Users = g.UsersGames
                        .Select(ug => new
                        {
                            ug.User.Username,
                            ug.User.IpAddress
                        })
                });

            var xmlGamesRoot = new XElement("games");
            var xmlGames = new XDocument(xmlGamesRoot);

            foreach (var game in finishedGames)
            {
                var xmlGame = new XElement("game", new XAttribute("name", game.Name));
                var xmlUsers = new XElement("users");
                if (game.Duration != null)
                {
                    var durationAttribute = new XAttribute("duration", game.Duration);
                    xmlGame.Add(durationAttribute);
                }

                foreach (var user in game.Users)
                {
                    var xmlUser = new XElement("user",
                        new XAttribute("username", user.Username),
                        new XAttribute("ip-address", user.IpAddress));
                    xmlUsers.Add(xmlUser);
                }

                xmlGame.Add(xmlUsers);
                xmlGames.Root.Add(xmlGame);
            }

            xmlGames.Save(filePath + fileName);

            Process.Start(filePath);
        }
        public static void Main()
        {
            var filePath = @"..\..\..\02.Export-Char-PlayedBy-Json";
            var fileName = @"\characters.json";

            var context = new DiabloEntities();

            var charsPlayedByUsers = context.Characters
                .Select(c => new
                {
                    name = c.Name,
                    playedBy = c.UsersGames.Select(g => g.User.Username)
                })
                .OrderBy(c => c.name)
                .ToList();

            var jsonResult = JsonConvert.SerializeObject(charsPlayedByUsers);
            File.WriteAllText(filePath + fileName, jsonResult);

            Process.Start(filePath);
        }
        public static void Main()
        {
            var dataFile = @"..\..\users-and-games.xml";
            var context = new DiabloEntities();
            var xmlDoc = XDocument.Load(dataFile);

            foreach (var xmlUser in xmlDoc.Root.Descendants("user"))
            {
                var userName = xmlUser.Attribute("username").Value;
                var existingUser = context.Users.FirstOrDefault(u => u.Username.Equals(userName));

                if (existingUser == null)
                {
                    AddUserGame(xmlUser, context);
                }
                else
                {
                    Console.WriteLine("User {0} already exists", userName);
                }

                context.SaveChanges();
            }
        }