Ejemplo n.º 1
0
        /// <summary>
        /// 初始化种子数据,添加超级管理组和超级管理员,默认账号:admin 111111
        /// </summary>
        /// <returns></returns>
        public async Task InitSeedDataAsync()
        {
            var defaultRole = await _sysRoleRepository.GetModelAsync(p => p.IsSuper);

            if (defaultRole != null)
            {
                throw new Exception("默认管理员组已存在");
            }

            var defaultUser = await _sysUserRepository.GetCountAsync(p => p.UserId > 0);

            if (defaultUser != 0)
            {
                throw new Exception("已有用户");
            }

            using (var uow = _fsq.CreateUnitOfWork())
            {
                _sysRoleRepository.UnitOfWork = uow;
                _sysUserRepository.UnitOfWork = uow;
                var userRoleRepo = uow.GetRepository <SysRoleUser>();
                userRoleRepo.UnitOfWork = uow;

                var role = new SysRole()
                {
                    IsSuper         = true,
                    RoleName        = "超级管理员组",
                    CreatedUserId   = 0,
                    CreatedUserName = "******",
                    UpdatedUserId   = 0,
                    UpdatedUserName = "******",
                };

                await _sysRoleRepository.InsertAsync(role);

                var user = new SysUser()
                {
                    LoginName       = "admin",
                    Password        = "******".EncryptMD5Encode(),
                    Status          = true,
                    UserName        = "******",
                    CreatedUserId   = 0,
                    CreatedUserName = "******",
                    UpdatedUserId   = 0,
                    UpdatedUserName = "******",
                };

                await _sysUserRepository.InsertAsync(user);

                var roleUser = new SysRoleUser()
                {
                    RoleId = role.RoleId,
                    UserId = user.UserId,
                };

                await userRoleRepo.InsertAsync(roleUser);

                uow.Commit();
            }
        }