private void InitializePortal(InstallPortalCommand command) { // Here, database created and initialize, hence UnitOfWork is used here using (var uow = UowFactory.CreateWithTransaction(IsolationLevel.ReadCommitted)) { // Add system admin user var sysAdminRole = uow.Context .Query <Entities.Security.Role>() .Where(r => r.RoleType == RoleTypeCodes.SystemAdmin && r.IsSystemRole) .SingleOrDefault(); _commandDispatcher.Send( new AddUserCommand { UserName = command.AdminUserName, Email = command.AdminUserName, IsSystemUser = true, RoleIds = new List <long> { sysAdminRole.Id }, }); var sysAdminUser = uow.Context .Query <Entities.Security.User>() .Single(u => u.IsSystemUser && u.UserName == command.AdminUserName); _commandDispatcher.Send( new ChangePasswordCommand { UserId = sysAdminUser.Id, PasswordHashAlgorithm = HashAlgorithm.PBKDF2.ToString(), NewPasswordHash = HashUtil.Hash(HashAlgorithm.PBKDF2, command.AdminPassword) }); _commandDispatcher.Send( new SetUserEmailConfirmCommand { UserId = sysAdminUser.Id, Confirmed = false, }); // Add portal uow.Context .Add(new Entities.Sites.Portal { Name = command.PortalName, Title = command.PortalName, Owner = sysAdminUser }); // Add default site _commandDispatcher.Send( new AddSiteCommand { SiteTitle = command.SiteName, IsDefault = true, IsActive = true, IsPublic = true, OwnerUsername = command.AdminUserName, }); string defaultSiteName = command.SiteName; var site = uow.Context .Query <Entities.Sites.Site>() .Single(s => s.Name == defaultSiteName); // Add default site zones foreach (var z in command.SiteZones) { _commandDispatcher.Send( new AddZoneCommand { SiteId = site.Id, Title = z.Title, ZoneType = z.Code, IsActive = true, IsPublic = true, Description = z.Description, }); } uow.Complete(); } }