Пример #1
0
        /// <summary>
        /// Scans the assembly for all migrations that can be applied to the
        /// given context, and adds them to the builder.
        /// </summary>
        /// <typeparam name="TContext">The context type</typeparam>
        /// <param name="builder">The builder to use</param>
        /// <param name="assembly">The assembly to scan</param>
        /// <returns>The builder instance</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static MigrationsBuilder <TContext> AddMigrationsFrom <TContext>(
            this MigrationsBuilder <TContext> builder, Assembly assembly)
            where TContext : IMigrationContext
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }
            if (assembly == null)
            {
                throw new ArgumentNullException(nameof(assembly));
            }

            var migrationBaseType = typeof(IMigration <TContext>).GetTypeInfo();

            var migrations = assembly.GetExportedTypes()
                             .Where(e =>
            {
                var t = e.GetTypeInfo();
                return(t.IsClass && !t.IsAbstract && migrationBaseType.IsAssignableFrom(t));
            });

            foreach (var migration in migrations)
            {
                builder.AddMigration(migration);
            }

            return(builder);
        }
Пример #2
0
        /// <summary>
        /// Configures this context to be used for relational databases
        /// </summary>
        /// <typeparam name="TContext">The context type</typeparam>
        /// <param name="builder">The migrations builder</param>
        /// <param name="connectionString">The connection string to be used</param>
        /// <param name="config">The configuration handler</param>
        /// <returns></returns>
        public static MigrationsBuilder <TContext> AddRelational <TContext>(
            this MigrationsBuilder <TContext> builder, string connectionString, Action <RelationalMigrationsBuilder <TContext> > config)
            where TContext : IRelationalMigrationContext
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }
            if (connectionString == null)
            {
                throw new ArgumentNullException(nameof(connectionString));
            }
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }
            if (string.IsNullOrWhiteSpace(connectionString))
            {
                throw new ArgumentException("Value cannot be whitespace.", nameof(connectionString));
            }

            var relationalBuilder = new RelationalMigrationsBuilder <TContext>(builder.ServiceCollection, connectionString);

            config(relationalBuilder);
            foreach (var migrationType in relationalBuilder.Migrations)
            {
                builder.AddMigration(migrationType);
            }

            return(builder);
        }
Пример #3
0
        /// <summary>
        /// Sets the <see cref="DefaultNamingNormalizer{TContext}"/> as the normalizer for
        /// this context.
        /// </summary>
        /// <typeparam name="TContext">The context type</typeparam>
        /// <param name="builder">The migrations builder</param>
        /// <returns>The builder instance</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static MigrationsBuilder <TContext> UseTrimNamingNormalizer <TContext>(
            this MigrationsBuilder <TContext> builder)
            where TContext : class, IMigrationContext
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            builder.ServiceCollection.AddSingleton <INamingNormalizer <TContext>, TrimNamingNormalizer <TContext> >();
            return(builder);
        }
Пример #4
0
        /// <summary>
        /// Scans the assemblies for all migrations that can be applied to the
        /// given context, and adds them to the builder.
        /// </summary>
        /// <typeparam name="TContext">The context type</typeparam>
        /// <param name="builder">The builder to use</param>
        /// <param name="assemblies">The assemblies to scan</param>
        /// <returns>The builder instance</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static MigrationsBuilder <TContext> AddMigrationsFrom <TContext>(
            this MigrationsBuilder <TContext> builder, params Assembly[] assemblies)
            where TContext : IMigrationContext
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }
            if (assemblies == null)
            {
                throw new ArgumentNullException(nameof(assemblies));
            }

            foreach (var assembly in assemblies)
            {
                builder.AddMigrationsFrom(assembly);
            }

            return(builder);
        }
        /// <summary>
        /// Adds support for migrations for the given <see cref="IMigrationContext"/>.
        /// </summary>
        /// <typeparam name="TContext">The migration context</typeparam>
        /// <param name="services">The service collection</param>
        /// <param name="config">Configuration handler</param>
        /// <returns>The service collection after changes</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static IServiceCollection AddMigrations <TContext>(
            this IServiceCollection services, Action <MigrationsBuilder <TContext> > config)
            where TContext : class, IMigrationContext
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            var builder = new MigrationsBuilder <TContext>(services);

            config(builder);

            services.TryAddSingleton <INamingNormalizer <TContext> >(new DefaultNamingNormalizer <TContext>());

            services.TryAddScoped <TContext>();
            foreach (var migrationType in builder.Migrations)
            {
                services.TryAddScoped(migrationType);
            }
            services.TryAddSingleton <IEnumerable <MigrationMetadata <TContext> > >(k =>
            {
                var normalizer = k.GetRequiredService <INamingNormalizer <TContext> >();

                var list = new List <MigrationMetadata <TContext> >(builder.Migrations.Count);
                list.AddRange(builder.Migrations.Select(e => new MigrationMetadata <TContext>(
                                                            normalizer.Normalize(e.Name), normalizer.Normalize(e.FullName), e)));

                return(list);
            });
            services.TryAddScoped <IMigrationRunner <TContext>, MigrationRunner <TContext> >();

            return(services);
        }
        /// <summary>
        /// Adds SQL Server support for the given <see cref="IRelationalMigrationContext"/>.
        /// </summary>
        /// <typeparam name="TContext">The context type</typeparam>
        /// <param name="builder">The migration builder</param>
        /// <param name="connectionString">The connection string</param>
        /// <param name="config">An optional configuration handler</param>
        /// <returns>The builder after changes</returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentException"></exception>
        public static MigrationsBuilder <TContext> AddSqlServer <TContext>(
            this MigrationsBuilder <TContext> builder, string connectionString, Action <SqlServerContextOptions <TContext> > config = null)
            where TContext : class, IRelationalMigrationContext
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            var options = new SqlServerContextOptions <TContext>(connectionString);

            config?.Invoke(options);
            builder.ServiceCollection.AddSingleton(k => options);
            builder.ServiceCollection.AddSingleton <SqlServerContextOptions>(
                k => k.GetRequiredService <SqlServerContextOptions <TContext> >());

            builder.ServiceCollection.AddScoped <SqlServerMigrationManager <TContext> >();
            builder.ServiceCollection.AddScoped <ISqlServerMigrationManager <TContext> >(
                k => k.GetRequiredService <SqlServerMigrationManager <TContext> >());
            builder.ServiceCollection.AddScoped <IMigrationManager <TContext> >(
                k => k.GetRequiredService <SqlServerMigrationManager <TContext> >());

            return(builder);
        }