예제 #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
        /// <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 SavedModelFiles ScaffoldContext(
            [NotNull] string provider,
            [NotNull] string connectionString,
            [CanBeNull] string outputDir,
            [CanBeNull] string outputContextDir,
            [CanBeNull] string dbContextClassName,
            [NotNull] IEnumerable <string> schemas,
            [NotNull] IEnumerable <string> tables,
            bool useDataAnnotations,
            bool overwriteFiles,
            bool useDatabaseNames)
        {
            Check.NotEmpty(provider, nameof(provider));
            Check.NotEmpty(connectionString, nameof(connectionString));
            Check.NotNull(schemas, nameof(schemas));
            Check.NotNull(tables, nameof(tables));

            outputDir = outputDir != null
                ? Path.GetFullPath(Path.Combine(_projectDir, outputDir))
                : _projectDir;

            outputContextDir = outputContextDir != null
                ? Path.GetFullPath(Path.Combine(_projectDir, outputContextDir))
                : outputDir;

            var services = _servicesBuilder.Build(provider);

            var scaffolder = services.GetRequiredService <IReverseEngineerScaffolder>();

            var @namespace = _rootNamespace;

            var subNamespace = SubnamespaceFromOutputPath(_projectDir, outputDir);

            if (!string.IsNullOrEmpty(subNamespace))
            {
                @namespace += "." + subNamespace;
            }

            var scaffoldedModel = scaffolder.ScaffoldModel(
                connectionString,
                tables,
                schemas,
                @namespace,
                _language,
                MakeDirRelative(outputDir, outputContextDir),
                dbContextClassName,
                new ModelReverseEngineerOptions
            {
                UseDatabaseNames = useDatabaseNames
            },
                new ModelCodeGenerationOptions
            {
                UseDataAnnotations = useDataAnnotations
            });

            return(scaffolder.Save(
                       scaffoldedModel,
                       outputDir,
                       overwriteFiles));
        }
        /// <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 ReverseEngineerFiles ScaffoldContext(
            [NotNull] string provider,
            [NotNull] string connectionString,
            [CanBeNull] string outputDir,
            [CanBeNull] string dbContextClassName,
            [NotNull] IEnumerable <string> schemas,
            [NotNull] IEnumerable <string> tables,
            bool useDataAnnotations,
            bool overwriteFiles,
            bool useDatabaseNames)
        {
            Check.NotEmpty(provider, nameof(provider));
            Check.NotEmpty(connectionString, nameof(connectionString));
            Check.NotNull(schemas, nameof(schemas));
            Check.NotNull(tables, nameof(tables));

            var services = _servicesBuilder.Build(provider);

            var generator = services.GetRequiredService <IReverseEngineerScaffolder>();

            return(generator.Generate(
                       connectionString,
                       tables,
                       schemas,
                       _projectDir,
                       outputDir,
                       _rootNamespace,
                       _language,
                       dbContextClassName,
                       useDataAnnotations,
                       overwriteFiles,
                       useDatabaseNames));
        }
예제 #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 SavedModelFiles ScaffoldContext(
            [NotNull] string provider,
            [NotNull] string connectionString,
            [CanBeNull] string outputDir,
            [CanBeNull] string outputContextDir,
            [CanBeNull] string dbContextClassName,
            [NotNull] IEnumerable <string> schemas,
            [NotNull] IEnumerable <string> tables,
            [CanBeNull] string modelNamespace,
            [CanBeNull] string contextNamespace,
            bool useDataAnnotations,
            bool overwriteFiles,
            bool useDatabaseNames,
            bool suppressOnConfiguring,
            bool noPluralize)
        {
            Check.NotEmpty(provider, nameof(provider));
            Check.NotEmpty(connectionString, nameof(connectionString));
            Check.NotNull(schemas, nameof(schemas));
            Check.NotNull(tables, nameof(tables));

            outputDir = outputDir != null
                ? Path.GetFullPath(Path.Combine(_projectDir, outputDir))
                : _projectDir;

            outputContextDir = outputContextDir != null
                ? Path.GetFullPath(Path.Combine(_projectDir, outputContextDir))
                : outputDir;

            var services = _servicesBuilder.Build(provider);

            var scaffolder = services.GetRequiredService <IReverseEngineerScaffolder>();

            var finalModelNamespace   = modelNamespace ?? GetNamespaceFromOutputPath(outputDir);
            var finalContextNamespace =
                contextNamespace ?? modelNamespace ?? GetNamespaceFromOutputPath(outputContextDir);

            var scaffoldedModel = scaffolder.ScaffoldModel(
                connectionString,
                new DatabaseModelFactoryOptions(tables, schemas),
                new ModelReverseEngineerOptions {
                UseDatabaseNames = useDatabaseNames, NoPluralize = noPluralize
            },
                new ModelCodeGenerationOptions
            {
                UseDataAnnotations    = useDataAnnotations,
                RootNamespace         = _rootNamespace,
                ModelNamespace        = finalModelNamespace,
                ContextNamespace      = finalContextNamespace,
                Language              = _language,
                ContextDir            = MakeDirRelative(outputDir, outputContextDir),
                ContextName           = dbContextClassName,
                SuppressOnConfiguring = suppressOnConfiguring
            });

            return(scaffolder.Save(
                       scaffoldedModel,
                       outputDir,
                       overwriteFiles));
        }
        public virtual Task <ReverseEngineerFiles> ScaffoldContextAsync(
            [NotNull] string provider,
            [NotNull] string connectionString,
            [CanBeNull] string outputDir,
            [CanBeNull] string dbContextClassName,
            [NotNull] IEnumerable <string> schemas,
            [NotNull] IEnumerable <string> tables,
            bool useDataAnnotations,
            bool overwriteFiles,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            Check.NotEmpty(provider, nameof(provider));
            Check.NotEmpty(connectionString, nameof(connectionString));
            Check.NotNull(schemas, nameof(schemas));
            Check.NotNull(tables, nameof(tables));

            var services = _servicesBuilder.Build(provider);

            var generator         = services.GetRequiredService <ReverseEngineeringGenerator>();
            var tableSelectionSet = new TableSelectionSet(tables, schemas);
            var configuration     = new ReverseEngineeringConfiguration
            {
                ConnectionString     = connectionString,
                ContextClassName     = dbContextClassName,
                ProjectPath          = _projectDir,
                ProjectRootNamespace = _rootNamespace,
                OutputPath           = outputDir,
                TableSelectionSet    = tableSelectionSet,
                UseFluentApiOnly     = !useDataAnnotations,
                OverwriteFiles       = overwriteFiles
            };

            return(generator.GenerateAsync(configuration, cancellationToken));
        }
예제 #7
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 SavedModelFiles ScaffoldContext(
            string provider,
            string connectionString,
            string?outputDir,
            string?outputContextDir,
            string?dbContextClassName,
            IEnumerable <string> schemas,
            IEnumerable <string> tables,
            string?modelNamespace,
            string?contextNamespace,
            bool useDataAnnotations,
            bool overwriteFiles,
            bool useDatabaseNames,
            bool suppressOnConfiguring,
            bool noPluralize)
        {
            outputDir = outputDir != null
                ? Path.GetFullPath(Path.Combine(_projectDir, outputDir))
                : _projectDir;

            outputContextDir = outputContextDir != null
                ? Path.GetFullPath(Path.Combine(_projectDir, outputContextDir))
                : outputDir;

            var services = _servicesBuilder.Build(provider);

            using var scope = services.CreateScope();

            var scaffolder = scope.ServiceProvider.GetRequiredService <IReverseEngineerScaffolder>();

            var finalModelNamespace   = modelNamespace ?? GetNamespaceFromOutputPath(outputDir);
            var finalContextNamespace =
                contextNamespace ?? modelNamespace ?? GetNamespaceFromOutputPath(outputContextDir);

            var scaffoldedModel = scaffolder.ScaffoldModel(
                connectionString,
                new DatabaseModelFactoryOptions(tables, schemas),
                new ModelReverseEngineerOptions {
                UseDatabaseNames = useDatabaseNames, NoPluralize = noPluralize
            },
                new ModelCodeGenerationOptions
            {
                UseDataAnnotations        = useDataAnnotations,
                RootNamespace             = _rootNamespace,
                ModelNamespace            = finalModelNamespace,
                ContextNamespace          = finalContextNamespace,
                Language                  = _language,
                UseNullableReferenceTypes = _nullable,
                ContextDir                = MakeDirRelative(outputDir, outputContextDir),
                ContextName               = dbContextClassName,
                SuppressOnConfiguring     = suppressOnConfiguring
            });

            return(scaffolder.Save(
                       scaffoldedModel,
                       outputDir,
                       overwriteFiles));
        }
예제 #8
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);
            }
        }
예제 #9
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));
        }
예제 #10
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 void Optimize(string?outputDir, string?modelNamespace, string?contextTypeName)
        {
            using var context = CreateContext(contextTypeName);
            var contextType = context.GetType();

            var services   = _servicesBuilder.Build(context);
            var scaffolder = services.GetRequiredService <ICompiledModelScaffolder>();

            if (outputDir == null)
            {
                var contextSubNamespace = contextType.Namespace ?? "";
                if (!string.IsNullOrEmpty(_rootNamespace) &&
                    contextSubNamespace.StartsWith(_rootNamespace, StringComparison.Ordinal))
                {
                    contextSubNamespace = contextSubNamespace[_rootNamespace.Length..];
예제 #11
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 ModelFiles ScaffoldContext(
            [NotNull] string provider,
            [NotNull] string connectionString,
            [CanBeNull] string outputDir,
            [CanBeNull] string dbContextClassName,
            [NotNull] IEnumerable <string> schemas,
            [NotNull] IEnumerable <string> tables,
            bool useDataAnnotations,
            bool overwriteFiles,
            bool useDatabaseNames)
        {
            Check.NotEmpty(provider, nameof(provider));
            Check.NotEmpty(connectionString, nameof(connectionString));
            Check.NotNull(schemas, nameof(schemas));
            Check.NotNull(tables, nameof(tables));

            var services = _servicesBuilder.Build(provider);

            var scaffolder = services.GetRequiredService <IReverseEngineerScaffolder>();

            var @namespace = _rootNamespace;

            var subNamespace = SubnamespaceFromOutputPath(_projectDir, outputDir);

            if (!string.IsNullOrEmpty(subNamespace))
            {
                @namespace += "." + subNamespace;
            }

            var scaffoldedModel = scaffolder.ScaffoldModel(
                connectionString,
                tables,
                schemas,
                @namespace,
                _language,
                dbContextClassName,
                useDataAnnotations,
                useDatabaseNames);

            return(scaffolder.Save(
                       scaffoldedModel,
                       _projectDir,
                       outputDir,
                       overwriteFiles));
        }
예제 #12
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 void Optimize(string?outputDir, string?modelNamespace, string?contextType)
        {
            using var context = CreateContext(contextType);

            var services   = _servicesBuilder.Build(context);
            var scaffolder = services.GetRequiredService <ICompiledModelScaffolder>();

            outputDir = outputDir != null
                ? Path.GetFullPath(Path.Combine(_projectDir, outputDir))
                : _projectDir;

            var finalModelNamespace = modelNamespace ?? GetNamespaceFromOutputPath(outputDir) ?? "";

            scaffolder.ScaffoldModel(
                context.GetService <IDesignTimeModel>().Model,
                outputDir,
                new CompiledModelCodeGenerationOptions
            {
                ContextType               = context.GetType(),
                ModelNamespace            = finalModelNamespace,
                Language                  = _language,
                UseNullableReferenceTypes = _nullable
            });

            var fullName = context.GetType().ShortDisplayName() + "Model";

            if (!string.IsNullOrEmpty(modelNamespace))
            {
                fullName = modelNamespace + "." + fullName;
            }

            _reporter.WriteInformation(DesignStrings.CompiledModelGenerated($"options.UseModel({fullName}.Instance)"));

            var cacheKeyFactory = context.GetService <IModelCacheKeyFactory>();

            if (!(cacheKeyFactory is ModelCacheKeyFactory))
            {
                _reporter.WriteWarning(DesignStrings.CompiledModelCustomCacheKeyFactory(cacheKeyFactory.GetType().ShortDisplayName()));
            }
        }