Esempio n. 1
0
        internal virtual void EnsureSeedData()
        {
            if (!_configurationDbContext.IdentityResources.Any())
            {
                var resources = JsonConvert.DeserializeObject <List <IdentityResource> >(
                    File.ReadAllText(Path.Combine(_options.SeedExampleDataPath, "data_resources_identity.json")));
                foreach (var resource in resources)
                {
                    _configurationDbContext.IdentityResources.Add(resource.ToEntity());
                }
                _configurationDbContext.SaveChanges();
            }

            if (!_configurationDbContext.ApiResources.Any())
            {
                var resources = JsonConvert.DeserializeObject <List <ApiResource> >(
                    File.ReadAllText(Path.Combine(_options.SeedExampleDataPath, "data_resources_api.json")));
                foreach (var resource in resources)
                {
                    _configurationDbContext.ApiResources.Add(resource.ToEntity());
                }
                _configurationDbContext.SaveChanges();
            }

            if (!_configurationDbContext.Clients.Any())
            {
                var clients = JsonConvert.DeserializeObject <List <Client> >(
                    File.ReadAllText(Path.Combine(_options.SeedExampleDataPath, "data_clients.json")));
                foreach (var client in clients)
                {
                    _configurationDbContext.Clients.Add(client.ToEntity());
                }
                _configurationDbContext.SaveChanges();
            }

            if (!_userAccountDbContext.UserAccounts.Any())
            {
                var userAccounts = JsonConvert.DeserializeObject <List <UserAccount> >(
                    File.ReadAllText(Path.Combine(_options.SeedExampleDataPath, "data_users.json")));
                foreach (var userAccount in userAccounts)
                {
                    _userAccountDbContext.UserAccounts.Add(userAccount.ToEntity());
                }
                _userAccountDbContext.SaveChanges();
            }
        }
        internal virtual void EnsureSeedData()
        {
            this._logger.LogDebug("Ensure Seed Data");

            string rootPath = _options.SeedExampleDataPath.GetFullPath(
                this._environment.ContentRootPath);

            if (!_configurationDbContext.IdentityResources.Any())
            {
                var path = Path.Combine(rootPath,
                                        "data_resources_identity.json");

                _logger.LogDebug($"Loading file: {path}");

                var resources = JsonConvert
                                .DeserializeObject <List <IdentityResource> >(
                    File.ReadAllText(path));

                foreach (var resource in resources)
                {
                    _configurationDbContext.IdentityResources
                    .Add(resource.ToEntity());
                }

                _configurationDbContext.SaveChanges();
                _logger.LogDebug("Saved Resource Identities");
            }

            if (!_configurationDbContext.ApiResources.Any())
            {
                var path = Path.Combine(rootPath, "data_resources_api.json");
                _logger.LogDebug($"Loading file: {path}");

                var resources = JsonConvert
                                .DeserializeObject <List <ApiResource> >(
                    File.ReadAllText(path));

                foreach (var resource in resources)
                {
                    _configurationDbContext.ApiResources
                    .Add(resource.ToEntity());
                }

                _configurationDbContext.SaveChanges();
                _logger.LogDebug("Saved Resource API");
            }

            if (!_configurationDbContext.Clients.Any())
            {
                var path = Path.Combine(rootPath, "data_clients.json");
                _logger.LogDebug($"Loading file: {path}");

                var clients = JsonConvert
                              .DeserializeObject <List <Client> >(File.ReadAllText(path));

                foreach (var client in clients)
                {
                    _configurationDbContext.Clients.Add(client.ToEntity());
                }
                _configurationDbContext.SaveChanges();
                _logger.LogDebug("Saved Clients");
            }

            if (!_userAccountDbContext.UserAccounts.Any())
            {
                var path = Path.Combine(rootPath, "data_users.json");
                _logger.LogDebug($"Loading file: {path}");

                var userAccounts = JsonConvert
                                   .DeserializeObject <List <UserAccount> >(
                    File.ReadAllText(path));

                foreach (var userAccount in userAccounts)
                {
                    _userAccountDbContext.UserAccounts
                    .Add(userAccount.ToEntity());
                }

                _userAccountDbContext.SaveChanges();
                _logger.LogDebug("Saved Users");
            }
        }