Esempio n. 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);
        }
Esempio n. 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);
        }