コード例 #1
0
        /// <summary>
        /// Adds the users to roles.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="seedingType">Type of the seeding.</param>
        private static void AddUsersToRoles(AuthenticationDbContext context,
                                            SeedingType seedingType)
        {
            List <IdentityUserRole <String> > identityUserRoles = IdentityUserRoleSeedData.GetIdentityUserRoles(seedingType);

            foreach (IdentityUserRole <String> identityUserRole in identityUserRoles)
            {
                Boolean foundUserRole = context.UserRoles.Any(a => a.RoleId == identityUserRole.RoleId && a.UserId == identityUserRole.UserId);

                if (!foundUserRole)
                {
                    context.UserRoles.Add(identityUserRole);
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Adds the users.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="seedingType">Type of the seeding.</param>
        private static void AddUsers(AuthenticationDbContext context,
                                     SeedingType seedingType)
        {
            List <IdentityUser> identityUsers = IdentityUserSeedData.GetIdentityUsers(seedingType);

            foreach (IdentityUser identityUser in identityUsers)
            {
                Boolean foundUser = context.Users.Any(a => a.UserName == identityUser.UserName);

                if (!foundUser)
                {
                    context.Users.Add(identityUser);
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// Adds the roles.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="seedingType">Type of the seeding.</param>
        private static void AddRoles(AuthenticationDbContext context,
                                     SeedingType seedingType)
        {
            List <IdentityRole> roles = RoleSeedData.GetIdentityRoles(seedingType);

            foreach (IdentityRole role in roles)
            {
                Boolean foundRole = context.Roles.Any(a => a.Name == role.Name);

                if (!foundRole)
                {
                    context.Roles.Add(role);
                }
            }
        }
コード例 #4
0
        /// <summary>
        /// Initialises the authentication database.
        /// </summary>
        /// <param name="authenticationDbContext">The authentication database context.</param>
        /// <param name="seedingType">Type of the seeding.</param>
        public static void InitialiseAuthenticationDatabase(AuthenticationDbContext authenticationDbContext,
                                                            SeedingType seedingType)
        {
            Boolean isDbInitialised = false;
            Int32   retryCounter    = 0;

            while (retryCounter < 20 && !isDbInitialised)
            {
                try
                {
                    if (authenticationDbContext.Database.IsSqlServer())
                    {
                        authenticationDbContext.Database.Migrate();
                    }

                    DatabaseSeeding.AddRoles(authenticationDbContext, seedingType);
                    DatabaseSeeding.AddUsers(authenticationDbContext, seedingType);
                    DatabaseSeeding.AddUsersToRoles(authenticationDbContext, seedingType);

                    authenticationDbContext.SaveChanges();

                    isDbInitialised = true;
                    break;
                }
                catch (Exception ex)
                {
                    retryCounter++;
                    Thread.Sleep(10000);
                }
            }

            if (!isDbInitialised)
            {
                String connString = authenticationDbContext.Database.GetDbConnection().ConnectionString;

                Exception newException = new Exception($"Error initialising Db with Connection String [{connString}]");
                throw newException;
            }
        }