コード例 #1
0
        protected List <IdentityUser> initUsers(FIFAServerContext context)
        {
            // Creating new user only if no one exists
            if (context.Users.Count() == 0)
            {
                // Initializing the roles for users
                IdentityRole role = context.Roles.Add(new IdentityRole(AuthenticationRoles.UserRole));
                context.Roles.Add(role);

                IdentityRole adminRole = context.Roles.Add(new IdentityRole(AuthenticationRoles.AdministratorRole));
                context.Roles.Add(adminRole);
                context.SaveChanges();

                // Creating generic users
                var users = new List <IdentityUser>
                {
                    new IdentityUser("user1"),
                    new IdentityUser("user2"),
                    new IdentityUser("admin")
                };

                initUser(users[0], role.Id);
                context.Users.Add(users[0]);
                initUser(users[1], role.Id);
                context.Users.Add(users[1]);

                // adding the Admin role to the admin user
                initUser(users[2], role.Id);
                users[2].Roles.Add(new IdentityUserRole {
                    RoleId = adminRole.Id, UserId = users[2].Id
                });
                context.Users.Add(users[2]);

                context.SaveChanges();
                return(users);
            }
            else
            {
                return(null);
            }
        }
コード例 #2
0
        // Generates matches and scores for a list of players
        private List <Match> seedMatchesScores(FIFAServerContext context, List <TeamPlayer> players, Int32 scoreId, Int32 matchId)
        {
            List <Match> createdMatches = new List <Match>();

            // generate League 1 games
            foreach (TeamPlayer p1 in players)
            {
                foreach (TeamPlayer p2 in players)
                {
                    // avoid adding match for player against himself
                    if (p1.Id != p2.Id)
                    {
                        //add match & score
                        Match match = new Match {
                            Id = matchId++
                        };
                        context.Matches.AddOrUpdate(
                            m => m.Id,
                            match
                            );
                        context.SaveChanges();
                        createdMatches.Add(match);

                        var scoreHomePlayer1 = new Score {
                            Id = scoreId++, MatchId = match.Id, Location = Location.Home, TeamPlayer = p1
                        };
                        var scoreAwayPlayer2 = new Score {
                            Id = scoreId++, MatchId = match.Id, Location = Location.Away, TeamPlayer = p2
                        };
                        context.Scores.AddOrUpdate(
                            s => s.Id,
                            scoreHomePlayer1, scoreAwayPlayer2
                            );
                        context.SaveChanges();
                    }
                }
            }
            return(createdMatches);
        }