예제 #1
0
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        public virtual MigrationFiles AddMigration(
            [NotNull] string name,
            [CanBeNull] string outputDir,
            [CanBeNull] string contextType)
        {
            Check.NotEmpty(name, nameof(name));

            if (outputDir != null)
            {
                outputDir = Path.GetFullPath(Path.Combine(_projectDir, outputDir));
            }

            var subNamespace = SubnamespaceFromOutputPath(outputDir);

            using (var context = _contextOperations.CreateContext(contextType))
            {
                var services = _servicesBuilder.Build(context);
                EnsureServices(services);
                EnsureMigrationsAssembly(services);

                var scaffolder = services.GetRequiredService <IMigrationsScaffolder>();
                var migration  = scaffolder.ScaffoldMigration(name, _rootNamespace, subNamespace, _language);
                var files      = scaffolder.Save(_projectDir, migration, outputDir);

                return(files);
            }
        }
예제 #2
0
        /// <summary>
        ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
        ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
        ///     any release. You should only use it directly in your code with extreme caution and knowing that
        ///     doing so can result in application failures when updating to a new Entity Framework Core release.
        /// </summary>
        public virtual MigrationFiles AddMigration(
            [NotNull] string name,
            [CanBeNull] string outputDir,
            [CanBeNull] string contextType)
        {
            Check.NotEmpty(name, nameof(name));

            if (outputDir != null)
            {
                outputDir = Path.GetFullPath(Path.Combine(_projectDir, outputDir));
            }

            var subNamespace = SubnamespaceFromOutputPath(outputDir);

            using var context = _contextOperations.CreateContext(contextType);
            var contextClassName = context.GetType().Name;

            if (string.Equals(name, contextClassName, StringComparison.Ordinal))
            {
                throw new OperationException(
                          DesignStrings.ConflictingContextAndMigrationName(name));
            }

            var services = _servicesBuilder.Build(context);

            EnsureServices(services);
            EnsureMigrationsAssembly(services);

            var scaffolder = services.GetRequiredService <IMigrationsScaffolder>();
            var migration  = scaffolder.ScaffoldMigration(name, _rootNamespace, subNamespace, _language);
            var files      = scaffolder.Save(_projectDir, migration, outputDir);

            return(files);
        }
예제 #3
0
        public void CreateContext_get_service()
        {
            var assembly   = MockAssembly.Create(typeof(Startup));
            var operations = new DbContextOperations(
                new TestOperationReporter(),
                assembly,
                assembly);

            operations.CreateContext(typeof(TestContext).FullName);
        }
예제 #4
0
        public virtual MigrationFiles AddMigration(
            [NotNull] string name,
            [CanBeNull] string outputDir,
            [CanBeNull] string contextType)
        {
            Check.NotEmpty(name, nameof(name));

            outputDir = string.IsNullOrWhiteSpace(outputDir) ? null : outputDir;
            var subNamespace = SubnamespaceFromOutputPath(outputDir);

            using (var context = _contextOperations.CreateContext(contextType))
            {
                var services = _servicesBuilder.Build(context);
                EnsureServices(services);

                var scaffolder = services.GetRequiredService <MigrationsScaffolder>();
                var migration  = scaffolder.ScaffoldMigration(name, _rootNamespace, subNamespace);
                var files      = scaffolder.Save(_projectDir, migration, outputDir);

                return(files);
            }
        }
예제 #5
0
        /// <summary>
        ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
        ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
        ///     any release. You should only use it directly in your code with extreme caution and knowing that
        ///     doing so can result in application failures when updating to a new Entity Framework Core release.
        /// </summary>
        public virtual MigrationFiles AddMigration(
            string name,
            string?outputDir,
            string?contextType,
            string? @namespace)
        {
            Check.NotEmpty(name, nameof(name));

            if (outputDir != null)
            {
                outputDir = Path.GetFullPath(Path.Combine(_projectDir, outputDir));
            }

            var subNamespace = SubnamespaceFromOutputPath(outputDir);

            using var context = _contextOperations.CreateContext(contextType);
            var contextClassName = context.GetType().Name;

            if (string.Equals(name, contextClassName, StringComparison.Ordinal))
            {
                throw new OperationException(
                          DesignStrings.ConflictingContextAndMigrationName(name));
            }

            var services = _servicesBuilder.Build(context);

            EnsureServices(services);
            EnsureMigrationsAssembly(services);

            using var scope = services.CreateScope();
            var scaffolder = scope.ServiceProvider.GetRequiredService <IMigrationsScaffolder>();
            var migration  =
                string.IsNullOrEmpty(@namespace)
                // TODO: Honor _nullable (issue #18950)
                    ? scaffolder.ScaffoldMigration(name, _rootNamespace ?? string.Empty, subNamespace, _language)
                    : scaffolder.ScaffoldMigration(name, null, @namespace, _language);

            return(scaffolder.Save(_projectDir, migration, outputDir));
        }