Exemple #1
0
        private static void PreConfigure <TDbContext>(
            ScorpioDbContextOptions options,
            DbContextConfigurationContext <TDbContext> context)
            where TDbContext : ScorpioDbContext <TDbContext>
        {
            foreach (var defaultPreConfigureAction in options.DefaultPreConfigureActions)
            {
                defaultPreConfigureAction.Invoke(context);
            }

            var preConfigureActions = options.PreConfigureActions.GetOrDefault(typeof(TDbContext));

            if (!preConfigureActions.IsNullOrEmpty())
            {
                foreach (var preConfigureAction in preConfigureActions)
                {
                    ((Action <DbContextConfigurationContext <TDbContext> >)preConfigureAction).Invoke(context);
                }
            }
        }
Exemple #2
0
        public static DbContextOptions <TDbContext> Create <TDbContext>(IServiceProvider serviceProvider, ScorpioDbContextOptionsBuilder <TDbContext> optionsBuilder)
            where TDbContext : ScorpioDbContext <TDbContext>
        {
            var creationContext = GetCreationContext <TDbContext>(serviceProvider);

            var context = new DbContextConfigurationContext <TDbContext>(
                creationContext.ConnectionString,
                serviceProvider,
                creationContext.ExistingConnection
                );

            context.DbContextOptions.UseApplicationServiceProvider(serviceProvider);

            var options = GetDbContextOptions <TDbContext>(serviceProvider);

            PreConfigure(options, context);
            Configure(options, context);
            optionsBuilder.OptionsActions.ForEach(action => action?.Invoke(context.DbContextOptions));
            return(context.DbContextOptions.Options);
        }
Exemple #3
0
        private static void Configure <TDbContext>(
            ScorpioDbContextOptions options,
            DbContextConfigurationContext <TDbContext> context)
            where TDbContext : ScorpioDbContext <TDbContext>
        {
            var configureAction = options.ConfigureActions.GetOrDefault(typeof(TDbContext));

            if (configureAction != null)
            {
                ((Action <DbContextConfigurationContext <TDbContext> >)configureAction).Invoke(context);
            }
            else if (options.DefaultConfigureAction != null)
            {
                options.DefaultConfigureAction.Invoke(context);
            }
            else
            {
                throw new ScorpioException(
                          $"No configuration found for {typeof(DbContext).AssemblyQualifiedName}! Use services.Configure<ScorpioDbContextOptions>(...) to configure it.");
            }
        }