// *******************************************************************
        // Public methods.
        // *******************************************************************

        #region Public methods

        /// <summary>
        /// This method applies seed data to the specified data-context.
        /// </summary>
        /// <param name="context">The data-context to use for the operation.</param>
        public static void ApplySeedData(
            this SecretDbContext context
            )
        {
            // Validate the parameters before attempting to use them.
            Guard.Instance().ThrowIfNull(context, nameof(context));

            // Add data to the tables.
            //context.SeedSecrets();
        }
Beispiel #2
0
        // *******************************************************************
        // Public methods.
        // *******************************************************************

        #region Public methods

        /// <summary>
        /// This method adds SQL Server repositories for the CG.Secrets library.
        /// </summary>
        /// <param name="serviceCollection">The service collection to use for
        /// the operation.</param>
        /// <param name="configuration">The configuration to use for the operation.</param>
        /// <param name="serviceLifetime">The service lifetime to use for the operation.</param>
        /// <returns>The value of the <paramref name="serviceCollection"/> parameter,
        /// for chaining calls together.</returns>
        public static IServiceCollection AddSqlServerRepositories(
            this IServiceCollection serviceCollection,
            IConfiguration configuration,
            ServiceLifetime serviceLifetime = ServiceLifetime.Scoped
            )
        {
            // Validate the parameters before attempting to use them.
            Guard.Instance().ThrowIfNull(serviceCollection, nameof(serviceCollection))
            .ThrowIfNull(configuration, nameof(configuration));

            // Register the EFCORE options.
            serviceCollection.ConfigureOptions <SecretRepositoryOptions>(
                configuration,
                out var repositoryOptions
                );

            // Register the data-context.
            serviceCollection.AddTransient <SecretDbContext>(serviceProvider =>
            {
                // Get the options from the DI container.
                var options = serviceProvider.GetRequiredService <IOptions <SecretRepositoryOptions> >();

                // Create the options builder.
                var builder = new DbContextOptionsBuilder <SecretDbContext>();

                // Configure the options.
                builder.UseSqlServer(options.Value.ConnectionString);

                // Create the data-context.
                var context = new SecretDbContext(builder.Options);

                // Return the data-context.
                return(context);
            });

            // Register the data-context factory.
            serviceCollection.Add <IDbContextFactory <SecretDbContext> >(serviceLifetime);

            // Register the repositories.
            serviceCollection.Add <ISecretRepository, SecretRepository>(serviceLifetime);

            // Return the service collection.
            return(serviceCollection);
        }
        // *******************************************************************
        // Private methods.
        // *******************************************************************

        #region Private methods

        /// <summary>
        /// This method applies seed data to the Secrets table.
        /// </summary>
        /// <param name="context">The data-context to use for the operation.</param>
        private static void SeedSecrets(
            this SecretDbContext context
            )
        {
            // Don't seed an already populated table.
            if (true == context.Secrets.Any())
            {
                return;
            }

            // Add data to the table.
            context.AddRange(new Models.Secret[]
            {
                new Models.Secret()
                {
                }
            });

            // Save the changes.
            context.SaveChanges();
        }