/// <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>
        protected virtual void AddNavigationProperties([NotNull] IMutableForeignKey foreignKey)
        {
            Check.NotNull(foreignKey, nameof(foreignKey));

            var dependentEndExistingIdentifiers             = ExistingIdentifiers(foreignKey.DeclaringEntityType);
            var dependentEndNavigationPropertyCandidateName =
                _candidateNamingService.GetDependentEndCandidateNavigationPropertyName(foreignKey);
            var dependentEndNavigationPropertyName =
                _cSharpUtilities.GenerateCSharpIdentifier(
                    dependentEndNavigationPropertyCandidateName,
                    dependentEndExistingIdentifiers,
                    singularizePluralizer: null,
                    uniquifier: NavigationUniquifier);

            foreignKey.SetDependentToPrincipal(dependentEndNavigationPropertyName);

            if (foreignKey.DeclaringEntityType.IsKeyless)
            {
                return;
            }

            var principalEndExistingIdentifiers             = ExistingIdentifiers(foreignKey.PrincipalEntityType);
            var principalEndNavigationPropertyCandidateName = foreignKey.IsSelfReferencing()
                ? string.Format(
                CultureInfo.CurrentCulture,
                SelfReferencingPrincipalEndNavigationNamePattern,
                dependentEndNavigationPropertyName)
                : _candidateNamingService.GetPrincipalEndCandidateNavigationPropertyName(
                foreignKey, dependentEndNavigationPropertyName);

            if (!foreignKey.IsUnique &&
                !foreignKey.IsSelfReferencing())
            {
                principalEndNavigationPropertyCandidateName = _options.NoPluralize
                    ? principalEndNavigationPropertyCandidateName
                    : _pluralizer.Pluralize(principalEndNavigationPropertyCandidateName);
            }

            var principalEndNavigationPropertyName =
                _cSharpUtilities.GenerateCSharpIdentifier(
                    principalEndNavigationPropertyCandidateName,
                    principalEndExistingIdentifiers,
                    singularizePluralizer: null,
                    uniquifier: NavigationUniquifier);

            foreignKey.SetPrincipalToDependent(principalEndNavigationPropertyName);
        }
        /// <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>
        protected virtual void AddNavigationProperties(IMutableForeignKey foreignKey)
        {
            if (foreignKey == null)
            {
                throw new ArgumentNullException(nameof(foreignKey));
            }

            var dependentEndExistingIdentifiers             = ExistingIdentifiers(foreignKey.DeclaringEntityType);
            var dependentEndNavigationPropertyCandidateName =
                _candidateNamingService.GetDependentEndCandidateNavigationPropertyName(foreignKey);
            var dependentEndNavigationPropertyName =
                _cSharpUtilities.GenerateCSharpIdentifier(
                    dependentEndNavigationPropertyCandidateName,
                    dependentEndExistingIdentifiers,
                    singularizePluralizer: null,
                    uniquifier: NavigationUniquifier);

            foreignKey.HasDependentToPrincipal(dependentEndNavigationPropertyName);

            var principalEndExistingIdentifiers             = ExistingIdentifiers(foreignKey.PrincipalEntityType);
            var principalEndNavigationPropertyCandidateName = foreignKey.IsSelfReferencing()
                ? string.Format(
                CultureInfo.CurrentCulture,
                SelfReferencingPrincipalEndNavigationNamePattern,
                dependentEndNavigationPropertyName)
                : _candidateNamingService.GetPrincipalEndCandidateNavigationPropertyName(
                foreignKey, dependentEndNavigationPropertyName);

            if (!foreignKey.IsUnique &&
                !foreignKey.IsSelfReferencing())
            {
                principalEndNavigationPropertyCandidateName = _pluralizer.Pluralize(principalEndNavigationPropertyCandidateName);
            }

            var principalEndNavigationPropertyName =
                _cSharpUtilities.GenerateCSharpIdentifier(
                    principalEndNavigationPropertyCandidateName,
                    principalEndExistingIdentifiers,
                    singularizePluralizer: null,
                    uniquifier: NavigationUniquifier);

            foreignKey.HasPrincipalToDependent(principalEndNavigationPropertyName);
        }
        /// <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 ScaffoldedModel ScaffoldModel(
            string connectionString,
            IEnumerable <string> tables,
            IEnumerable <string> schemas,
            string @namespace,
            string language,
            string contextDir,
            string contextName,
            bool useDataAnnotations,
            bool useDatabaseNames)
        {
            Check.NotEmpty(connectionString, nameof(connectionString));
            Check.NotNull(tables, nameof(tables));
            Check.NotNull(schemas, nameof(schemas));
            Check.NotEmpty(@namespace, nameof(@namespace));
            Check.NotNull(language, nameof(language));

            if (!string.IsNullOrWhiteSpace(contextName) &&
                (!_cSharpUtilities.IsValidIdentifier(contextName) ||
                 _cSharpUtilities.IsCSharpKeyword(contextName)))
            {
                throw new ArgumentException(
                          DesignStrings.ContextClassNotValidCSharpIdentifier(contextName));
            }

            var databaseModel = _databaseModelFactory.Create(connectionString, tables, schemas);
            var model         = _factory.Create(databaseModel, useDatabaseNames);

            if (model == null)
            {
                throw new InvalidOperationException(
                          DesignStrings.ProviderReturnedNullModel(
                              _factory.GetType().ShortDisplayName()));
            }

            if (string.IsNullOrEmpty(contextName))
            {
                contextName = DefaultDbContextName;

                var annotatedName = model.Scaffolding().DatabaseName;
                if (!string.IsNullOrEmpty(annotatedName))
                {
                    contextName = _cSharpUtilities.GenerateCSharpIdentifier(
                        annotatedName + DbContextSuffix,
                        existingIdentifiers: null,
                        singularizePluralizer: null);
                }
            }

            var codeGenerator = ModelCodeGeneratorSelector.Select(language);

            return(codeGenerator.GenerateModel(model, @namespace, contextDir ?? string.Empty, contextName, connectionString, useDataAnnotations));
        }
Example #4
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 string GetName([NotNull] T item)
        {
            Check.NotNull(item, nameof(item));

            if (NameCache.ContainsKey(item))
            {
                return(NameCache[item]);
            }

            var name = _cSharpUtilities.GenerateCSharpIdentifier(
                _nameGetter(item), existingIdentifiers: null, singularizePluralizer: _singularizePluralizer);

            NameCache.Add(item, name);
            return(name);
        }
Example #5
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 ReverseEngineerFiles Generate(
            string connectionString,
            IEnumerable <string> tables,
            IEnumerable <string> schemas,
            string projectPath,
            string outputPath,
            string rootNamespace,
            string contextName,
            bool useDataAnnotations,
            bool overwriteFiles,
            bool useDatabaseNames)
        {
            if (connectionString == null)
            {
                throw new ArgumentNullException(nameof(connectionString));
            }
            if (tables == null)
            {
                throw new ArgumentNullException(nameof(tables));
            }
            if (schemas == null)
            {
                throw new ArgumentNullException(nameof(schemas));
            }
            if (projectPath == null)
            {
                throw new ArgumentNullException(nameof(projectPath));
            }
            if (rootNamespace == null)
            {
                throw new ArgumentNullException(nameof(rootNamespace));
            }

            if (!string.IsNullOrWhiteSpace(contextName) &&
                (!_cSharpUtilities.IsValidIdentifier(contextName) ||
                 _cSharpUtilities.IsCSharpKeyword(contextName)))
            {
                throw new ArgumentException(
                          DesignStrings.ContextClassNotValidCSharpIdentifier(contextName));
            }

            var databaseModel = _databaseModelFactory.Create(connectionString, tables, schemas);
            //var model = _factory.Create(connectionString, tables, schemas, useDatabaseNames);
            var model = _factory.Create(databaseModel, useDatabaseNames);

            if (model == null)
            {
                throw new InvalidOperationException(
                          DesignStrings.ProviderReturnedNullModel(
                              _factory.GetType().ShortDisplayName()));
            }

            outputPath = string.IsNullOrWhiteSpace(outputPath) ? null : outputPath;
            var subNamespace = SubnamespaceFromOutputPath(projectPath, outputPath);

            var @namespace = rootNamespace;

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

            if (string.IsNullOrEmpty(contextName))
            {
                contextName = DefaultDbContextName;

                var annotatedName = model.Scaffolding().DatabaseName;
                if (!string.IsNullOrEmpty(annotatedName))
                {
                    contextName = _cSharpUtilities.GenerateCSharpIdentifier(
                        annotatedName + DbContextSuffix,
                        existingIdentifiers: null,
                        singularizePluralizer: null);
                }
            }

            CheckOutputFiles(outputPath ?? projectPath, contextName, model, overwriteFiles);

            return(ScaffoldingCodeGenerator.WriteCode(model, outputPath ?? projectPath, @namespace, contextName, connectionString, useDataAnnotations));
        }
        /// <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 Generate(
            string connectionString,
            IEnumerable <string> tables,
            IEnumerable <string> schemas,
            string projectPath,
            string outputPath,
            string rootNamespace,
            string contextName,
            bool useDataAnnotations,
            bool overwriteFiles,
            bool useDatabaseNames)
        {
            Check.NotEmpty(connectionString, nameof(connectionString));
            Check.NotNull(tables, nameof(tables));
            Check.NotNull(schemas, nameof(schemas));
            Check.NotEmpty(projectPath, nameof(projectPath));
            Check.NotEmpty(rootNamespace, nameof(rootNamespace));

            if (!string.IsNullOrWhiteSpace(contextName) &&
                (!_cSharpUtilities.IsValidIdentifier(contextName) ||
                 _cSharpUtilities.IsCSharpKeyword(contextName)))
            {
                throw new ArgumentException(
                          DesignStrings.ContextClassNotValidCSharpIdentifier(contextName));
            }

            var databaseModel = _databaseModelFactory.Create(connectionString, tables, schemas);
            var model         = _factory.Create(databaseModel, useDatabaseNames);

            if (model == null)
            {
                throw new InvalidOperationException(
                          DesignStrings.ProviderReturnedNullModel(
                              _factory.GetType().ShortDisplayName()));
            }

            projectPath = projectPath.TrimEnd(_directorySeparatorChars);

            var fullProjectPath = Path.GetFullPath(projectPath);
            var fullOutputPath  = string.IsNullOrEmpty(outputPath)
                ? fullProjectPath
                : Path.GetFullPath(Path.Combine(projectPath, outputPath));

            var @namespace = rootNamespace;

            if (!string.Equals(fullOutputPath, fullProjectPath) &&
                fullOutputPath.StartsWith(fullProjectPath, StringComparison.Ordinal))
            {
                var relativeOutputPath = fullOutputPath.Substring(fullProjectPath.Length + 1);
                @namespace += "." + string.Join(
                    ".", relativeOutputPath
                    .Split(_directorySeparatorChars, StringSplitOptions.RemoveEmptyEntries)
                    .Select(
                        p => _cSharpUtilities.GenerateCSharpIdentifier(
                            p,
                            existingIdentifiers: null,
                            singularizePluralizer: null)));
            }

            if (string.IsNullOrEmpty(contextName))
            {
                contextName = DefaultDbContextName;

                var annotatedName = model.Scaffolding().DatabaseName;
                if (!string.IsNullOrEmpty(annotatedName))
                {
                    contextName = _cSharpUtilities.GenerateCSharpIdentifier(
                        annotatedName + DbContextSuffix,
                        existingIdentifiers: null,
                        singularizePluralizer: null);
                }
            }

            CheckOutputFiles(fullOutputPath, contextName, model, overwriteFiles);

            return(ScaffoldingCodeGenerator.WriteCode(model, fullOutputPath, @namespace, contextName, connectionString, useDataAnnotations));
        }