/// <summary>
        /// Generate entity type class.
        /// </summary>
        /// <param name="entityType">Represents an entity type in an <see cref="T:Microsoft.EntityFrameworkCore.Metadata.IModel" />.</param>
        /// <param name="namespace">Entity type namespace.</param>
        /// <param name="useDataAnnotations">If true use data annotations.</param>
        /// <returns>Generated entity type.</returns>
        public override string WriteCode(IEntityType entityType, string @namespace, bool useDataAnnotations)
        {
            Check.NotNull(entityType, nameof(entityType));
            Check.NotNull(@namespace, nameof(@namespace));

            UseDataAnnotations = useDataAnnotations;
            TemplateData       = new Dictionary <string, object>();

            if (_options.Value.TemplateData != null)
            {
                foreach (KeyValuePair <string, object> entry in _options.Value.TemplateData)
                {
                    TemplateData.Add(entry.Key, entry.Value);
                }
            }

            TemplateData.Add("use-data-annotations", UseDataAnnotations);

            GenerateImports(entityType);

            TemplateData.Add("namespace", @namespace);

            GenerateClass(entityType);

            string output = EntityTypeTemplateService.GenerateEntityType(TemplateData);

            return(output);
        }
        /// <summary>
        /// Generate entity type class.
        /// </summary>
        /// <param name="entityType">Represents an entity type in an <see cref="T:Microsoft.EntityFrameworkCore.Metadata.IModel" />.</param>
        /// <param name="namespace">Entity type namespace.</param>
        /// <param name="useDataAnnotations">If true use data annotations.</param>
        /// <returns>Generated entity type.</returns>
        public virtual string WriteCode(IEntityType entityType, string @namespace, bool useDataAnnotations)
        {
            if (entityType == null)
            {
                throw new ArgumentNullException(nameof(entityType));
            }
            if (@namespace == null)
            {
                throw new ArgumentNullException(nameof(@namespace));
            }

            UseDataAnnotations = useDataAnnotations;
            TemplateData       = new Dictionary <string, object>();
            TemplateData.Add("use-data-annotations", UseDataAnnotations);

            GenerateImports(entityType);

            TemplateData.Add("namespace", @namespace);

            GenerateClass(entityType);

            string output = EntityTypeTemplateService.GenerateEntityType(TemplateData);

            return(output);
        }
        /// <summary>
        /// Generate entity type class.
        /// </summary>
        /// <param name="entityType">Represents an entity type in an <see cref="T:Microsoft.EntityFrameworkCore.Metadata.IModel" />.</param>
        /// <param name="namespace">Entity type namespace.</param>
        /// <param name="useDataAnnotations">If true use data annotations.</param>
        /// <returns>Generated entity type.</returns>
        public override string WriteCode(IEntityType entityType, string @namespace, bool useDataAnnotations)
        {
            Check.NotNull(entityType, nameof(entityType));
            Check.NotNull(@namespace, nameof(@namespace));

            UseDataAnnotations = useDataAnnotations;
            TemplateData       = new Dictionary <string, object>();

            if (_options.Value.TemplateData != null)
            {
                foreach (KeyValuePair <string, object> entry in _options.Value.TemplateData)
                {
                    TemplateData.Add(entry.Key, entry.Value);
                }
            }

            TemplateData.Add("use-data-annotations", UseDataAnnotations);

            GenerateImports(entityType);

            // TODO: _sb.AppendLine("#nullable disable");
            @namespace = _options?.Value?.EnableSchemaFolders == true
                ? $"{@namespace}.{CSharpHelper.Namespace(entityType.GetSchema())}" : @namespace;

            TemplateData.Add("namespace", @namespace);

            GenerateClass(entityType);

            string output = EntityTypeTemplateService.GenerateEntityType(TemplateData);

            return(output);
        }
Beispiel #4
0
        /// <summary>
        /// <summary>Generates code for a model.</summary>
        /// </summary>
        /// <param name="model"> The model.</param>
        /// <param name="options"> The options to use during generation. </param>
        /// <returns> The generated model. </returns>
        public override ScaffoldedModel GenerateModel(IModel model, ModelCodeGenerationOptions options)
        {
            Check.NotNull(model, nameof(model));
            Check.NotNull(options, nameof(options));

            // Register Hbs helpers and partial templates
            HandlebarsHelperService.RegisterHelpers();
            HandlebarsBlockHelperService.RegisterBlockHelpers();
            DbContextTemplateService.RegisterPartialTemplates();
            EntityTypeTemplateService.RegisterPartialTemplates();

            var resultingFiles = new ScaffoldedModel();

            string generatedCode;

            if (!(CSharpDbContextGenerator is NullCSharpDbContextGenerator))
            {
                generatedCode = CSharpDbContextGenerator.WriteCode(
                    model,
                    options.ContextName,
                    options.ConnectionString,
                    options.ContextNamespace,
                    options.ModelNamespace,
                    options.UseDataAnnotations,
                    options.SuppressConnectionStringWarning);

                var dbContextFileName = options.ContextName + FileExtension;
                resultingFiles.ContextFile = new ScaffoldedFile
                {
                    Path = options.ContextDir != null
                        ? Path.Combine(options.ContextDir, dbContextFileName)
                        : dbContextFileName,
                    Code = generatedCode
                };
            }

            if (!(CSharpEntityTypeGenerator is NullCSharpEntityTypeGenerator))
            {
                foreach (var entityType in model.GetScaffoldEntityTypes(_options.Value))
                {
                    generatedCode = CSharpEntityTypeGenerator.WriteCode(
                        entityType,
                        options.ModelNamespace,
                        options.UseDataAnnotations);

                    var transformedFileName = EntityTypeTransformationService.TransformEntityFileName(entityType.DisplayName());
                    var entityTypeFileName  = transformedFileName + FileExtension;
                    resultingFiles.AdditionalFiles.Add(
                        new ScaffoldedFile
                    {
                        Path = entityTypeFileName,
                        Code = generatedCode
                    });
                }
            }

            return(resultingFiles);
        }
Beispiel #5
0
        /// <summary>
        /// Reverse engineer DbContext and entity type files from an existing database.
        /// </summary>
        /// <param name="model">Metadata about the shape of entities, the relationships between them, and how they map to the database.</param>
        /// <param name="outputPath">File path for the generated files.</param>
        /// <param name="namespace">Namespace for generated classes.</param>
        /// <param name="contextName">DbContext name.</param>
        /// <param name="connectionString">Database connection string.</param>
        /// <param name="useDataAnnotations">Generate classes using data annotations.</param>
        /// <returns></returns>
        public override ReverseEngineerFiles WriteCode(
            IModel model,
            string outputPath,
            string @namespace,
            string contextName,
            string connectionString,
            bool useDataAnnotations)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            if (outputPath == null)
            {
                throw new ArgumentNullException(nameof(outputPath));
            }
            if (@namespace == null)
            {
                throw new ArgumentNullException(nameof(@namespace));
            }
            if (contextName == null)
            {
                throw new ArgumentNullException(nameof(contextName));
            }
            if (connectionString == null)
            {
                throw new ArgumentNullException(nameof(connectionString));
            }

            // Register Hbs helpers and partial templates
            DbContextTemplateService.RegisterHelper(Constants.SpacesHelper, HandlebarsHelpers.GetSpacesHelper());
            DbContextTemplateService.RegisterPartialTemplates();
            EntityTypeTemplateService.RegisterPartialTemplates();

            ReverseEngineerFiles reverseEngineerFiles = new ReverseEngineerFiles();

            if (!(CSharpDbContextGenerator is NullCSharpDbContextGenerator))
            {
                string contents1 = CSharpDbContextGenerator.WriteCode(model, @namespace, contextName, connectionString, useDataAnnotations);
                string fileName1 = contextName + FileExtension;
                string str1      = FileService.OutputFile(outputPath, fileName1, contents1);
                reverseEngineerFiles.ContextFile = str1;
            }

            if (!(CSharpEntityTypeGenerator is NullCSharpEntityTypeGenerator))
            {
                foreach (IEntityType entityType in model.GetEntityTypes())
                {
                    string contents2 = CSharpEntityTypeGenerator.WriteCode(entityType, @namespace, useDataAnnotations);
                    string fileName2 = entityType.DisplayName() + FileExtension;
                    string str2      = FileService.OutputFile(outputPath, fileName2, contents2);
                    reverseEngineerFiles.EntityTypeFiles.Add(str2);
                }
            }

            return(reverseEngineerFiles);
        }
Beispiel #6
0
        /// <summary>
        /// Generate entity type class.
        /// </summary>
        /// <param name="entityType">Represents an entity type in an <see cref="T:Microsoft.EntityFrameworkCore.Metadata.IModel" />.</param>
        /// <param name="namespace">Entity type namespace.</param>
        /// <param name="useDataAnnotations">If true use data annotations.</param>
        /// <returns>Generated entity type.</returns>
        public override string WriteCode(IEntityType entityType, string @namespace, bool useDataAnnotations)
        {
            Check.NotNull(entityType, nameof(entityType));
            Check.NotNull(@namespace, nameof(@namespace));

            TemplateData = new Dictionary <string, object>();
            GenerateImports(entityType);
            GenerateClass(entityType);

            string output = EntityTypeTemplateService.GenerateEntityType(TemplateData);

            return(output);
        }
Beispiel #7
0
        /// <summary>Generates code for a model.</summary>
        /// <param name="model"> The model. </param>
        /// <param name="namespace"> The namespace. </param>
        /// <param name="contextDir"> The directory of the <see cref="T:Microsoft.EntityFrameworkCore.DbContext" />. </param>
        /// <param name="contextName"> The name of the <see cref="T:Microsoft.EntityFrameworkCore.DbContext" />. </param>
        /// <param name="connectionString"> The connection string. </param>
        /// <param name="options"> The options to use during generation. </param>
        /// <returns> The generated model. </returns>
        public override ScaffoldedModel GenerateModel(IModel model,
                                                      string @namespace,
                                                      string contextDir,
                                                      string contextName,
                                                      string connectionString,
                                                      ModelCodeGenerationOptions options)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            if (string.IsNullOrWhiteSpace(@namespace))
            {
                throw new ArgumentNullException(nameof(@namespace));
            }
            if (contextDir == null)
            {
                throw new ArgumentNullException(nameof(contextDir));
            }
            if (string.IsNullOrWhiteSpace(contextName))
            {
                throw new ArgumentNullException(nameof(contextName));
            }
            if (string.IsNullOrWhiteSpace(connectionString))
            {
                throw new ArgumentNullException(nameof(connectionString));
            }
            if (contextDir == null)
            {
                throw new ArgumentNullException(nameof(contextDir));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            // Register Hbs helpers and partial templates
            HandlebarsHelperService.RegisterHelpers();
            DbContextTemplateService.RegisterPartialTemplates();
            EntityTypeTemplateService.RegisterPartialTemplates();

            var resultingFiles = new ScaffoldedModel();

            string generatedCode;

            if (!(CSharpDbContextGenerator is NullCSharpDbContextGenerator))
            {
                generatedCode = CSharpDbContextGenerator.WriteCode(model, @namespace, contextName, connectionString, options.UseDataAnnotations, options.SuppressConnectionStringWarning);

                var dbContextFileName = contextName + FileExtension;
                resultingFiles.ContextFile = new ScaffoldedFile {
                    Path = Path.Combine(contextDir, dbContextFileName), Code = generatedCode
                };
            }

            if (!(CSharpEntityTypeGenerator is NullCSharpEntityTypeGenerator))
            {
                foreach (var entityType in model.GetEntityTypes())
                {
                    generatedCode = CSharpEntityTypeGenerator.WriteCode(entityType, @namespace, options.UseDataAnnotations);

                    var transformedFileName = EntityTypeTransformationService.TransformEntityFileName(entityType.DisplayName());
                    var entityTypeFileName  = transformedFileName + FileExtension;
                    resultingFiles.AdditionalFiles.Add(new ScaffoldedFile {
                        Path = entityTypeFileName, Code = generatedCode
                    });
                }
            }

            return(resultingFiles);
        }