コード例 #1
0
        protected virtual FileBuilder ResolveContextFileBuilder()
        {
            FileBuilder ret        = null;
            var         outputDir  = Options.ContextOutputDir ?? Options.OutputDir;
            var         outputFile = Options.ContextOutputSingleFileName ?? Options.OutputSingleFileName ?? $"{ContextClassName()}.generated.cs";

            GenerationContext.File($"{outputDir}{Path.DirectorySeparatorChar}{outputFile}", fb => ret = fb);
            return(ret);
        }
コード例 #2
0
        protected virtual FileBuilder ResolveModelExtensionFileBuilder(ITable table)
        {
            FileBuilder ret        = null;
            var         outputDir  = Options.ModelExtensionsOutputDir ?? Options.OutputDir;
            var         outputFile = Options.ModelExtensionsOutputSingleFileName ?? Options.OutputSingleFileName ?? $"{ModelExtensionsClassName(table)}.generated.cs";

            GenerationContext.File($"{outputDir}{Path.DirectorySeparatorChar}{outputFile}", fb => ret = fb);
            return(ret);
        }
コード例 #3
0
        protected virtual FileBuilder ResolveEntityInterfaceFileBuilder(ITable table)
        {
            FileBuilder ret        = null;
            var         outputDir  = Options.EntitiesInterfacesOutputDir ?? Options.OutputDir;
            var         outputFile = Options.EntitiesInterfacesOutputSingleFileName ?? Options.OutputSingleFileName ?? $"{TableInterfaceName(table)}.generated.cs";

            GenerationContext.File($"{outputDir}{Path.DirectorySeparatorChar}{outputFile}", fb => ret = fb);
            return(ret);
        }
コード例 #4
0
 private void GenerateFluentConfigurations()
 {
     TablesToGenerate.ForEach(table =>
     {
         if (Options.OutputToSingleFile)
         {
             GenerationContext.SingleFile(fb => GenerateEntityFluentConfiguration(table, fb));
         }
         else
         {
             GenerationContext.File(fb => GenerateEntityFluentConfiguration(table, fb));
         }
     });
 }
コード例 #5
0
        // TODO refactor. move to SQL SERVER EF6
        /// <summary>
        /// //method.RawLine($"return Database.SqlQuery<{outputType}>(\"SELECT NEXT VALUE FOR [{sequence.Schema}].[{sequence.Name}];\").First()");
        /// </summary>
        /// <param name="method"></param>
        /// <param name="outputType"></param>
        /// <param name="sequence"></param>
        //protected abstract void GenerateGetNextSequenceLines(MethodBuilder method, string outputType, ISequence sequence);



        private void GenerateEntityFluentConfiguration(ITable table)
        {
            var tableNamespace = TableNamespace(table);
            var tableFluentConfigurationClassName = $"{TableClassName(table)}{Options.FluentConfigurationClassSuffix}";
            var tableClassName     = TableClassName(table);
            var tableClassFullName = TableClassFullName(table);
            var entityClass        = GenerationContext.FindClass(tableClassName, tableNamespace);
            var contextNamespace   = ContextNamespace();

            // set the path.
            var outputDir = Options.ContextOutputDir ?? Options.OutputDir;
            var fileName  = Options.ContextOutputSingleFileName ?? Options.OutputSingleFileName ?? $"{tableFluentConfigurationClassName}.generated.cs";
            var path      = $"{outputDir}{Path.DirectorySeparatorChar}{fileName}";

            GenerationContext.File(path, fileBuilder =>
            {
                // set the namespace.
                fileBuilder.Namespace(contextNamespace, true, ns =>
                {
                    ns.Class(tableFluentConfigurationClassName, true, fluentConfigClass =>
                    {
                        fluentConfigClass
                        .Partial(true)
                        .Inherits($"System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<{tableClassFullName}>")
                        .Constructor(constructor =>
                        {
                            constructor.AddComment("Table mapping & keys");

                            // to table mapping.
                            constructor.RawLine(ToTableFluent(table));

                            if (table.Columns.Count(t => t.IsPrimaryKey) > 1)
                            {
                                //HasKey(x => new { x.PurchaseOrderId, x.RequiredDocumentId });
                                var props = string.Join(", ", table.Columns
                                                        .Where(t => t.IsPrimaryKey)
                                                        .OrderBy(t => t.PrimaryKeyOrder)
                                                        .Select(t => $"x.{entityClass.FindByMeta<PropertyBuilder>(t).GetName()}"));

                                var line = $"HasKey(x => new {{ {props} }})";
                                constructor.RawLine(line);
                            }
                            else
                            {
                                // pk mapping.
                                var pk     = table.Columns.FirstOrDefault(t => t.IsPrimaryKey);
                                var pkProp = entityClass.FindByMeta <PropertyBuilder>(pk);
                                constructor.RawLine($"HasKey(t => t.{pkProp.GetName()})");
                            }

                            constructor.AddComment("Columns");

                            // columns mapping.
                            table.Columns.ForEach(column =>
                            {
                                var columnProp = entityClass.FindByMeta <PropertyBuilder>(column);
                                var columnLine = RawLineBuilder.Create();
                                columnLine.Append($"Property(t => t.{columnProp.GetName()})");
                                columnLine.Append($".HasColumnName(\"{column.Name}\")");

                                if (column.IsNullable)
                                {
                                    columnLine.Append(".IsOptional()");
                                }
                                else
                                {
                                    columnLine.Append(".IsRequired()");
                                }

                                columnLine.Append($".HasColumnType(\"{column.DataType}\")");

                                if (column.IsPrimaryKey)
                                {
                                    if (IsGenerateOptionIdentity(column))
                                    {
                                        columnLine.Append(".HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)");
                                    }
                                    else
                                    {
                                        columnLine.Append(".HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None)");
                                    }

                                    /*
                                     * // TODO make overridable class method here called IsGenerateOptionIdentity(IColumn)
                                     * if (column.IsAutoIncrement)
                                     *  columnLine.Append(".HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)");
                                     * else if (column.DataType == "uniqueidentifier" && column.DefaultValue.IndexOf("newsequentialid", StringComparison.InvariantCultureIgnoreCase) > -1)
                                     *  columnLine.Append(".HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)");
                                     * else
                                     *  columnLine.Append(".HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None)");
                                     */
                                }

                                if (column.CharacterMaximumLength.HasValue && column.CharacterMaximumLength != -1)
                                {
                                    columnLine.Append($".HasMaxLength({column.CharacterMaximumLength})");
                                }

                                if (DataTypeResolver.IsFixLength(column))
                                {
                                    columnLine.Append(".IsFixedLength()");
                                }

                                if (DataTypeResolver.IsString(column) && !DataTypeResolver.IsUnicode(column))
                                {
                                    columnLine.Append(".IsUnicode(false)");
                                }

                                if (column.NumericPrecision.HasValue && column.NumericScale.HasValue && column.NumericScale != 0)
                                {
                                    columnLine.Append($".HasPrecision({column.NumericPrecision}, {column.NumericScale})");
                                }


                                constructor.Add(columnLine);
                            });

                            constructor.AddComment("Navigations");
                            table.ForeignKeys.ForEach(fk =>
                            {
                                var fkProp       = FindNavigation(entityClass, fk);
                                var fkColumnProp = entityClass.FindByMeta <PropertyBuilder>(fk.ForeignKeyColumn);

                                // if null meaning its being filtered. (excluded table from generation)
                                if (fkProp != null)
                                {
                                    var line             = RawLineBuilder.Create();
                                    var primaryNamespace = TableNamespace(fk.PrimaryKeyColumn.Table);
                                    var primaryClassName = TableClassName(fk.PrimaryKeyColumn.Table);
                                    var primaryEntity    = GenerationContext.FindClass(primaryClassName, primaryNamespace);

                                    PropertyBuilder reverseNav;
                                    if (fk.PrimaryKeyColumn.Table == fk.ForeignKeyColumn.Table)
                                    {
                                        reverseNav = FindNavigation(primaryEntity, fk, NavigationKind.HasMany);
                                    }
                                    else
                                    {
                                        reverseNav = FindNavigation(primaryEntity, fk);
                                    }

                                    if (fk.ForeignKeyColumn.IsNullable)
                                    {
                                        line.Append($"HasOptional(t => t.{fkProp.GetName()})");
                                    }
                                    else
                                    {
                                        line.Append($"HasRequired(t => t.{fkProp.GetName()})");
                                    }

                                    if (fk.IsOneToOne())
                                    {
                                        line.Append($".WithOptional(t => t.{reverseNav.GetName()})");
                                    }
                                    else
                                    {
                                        line.Append($".WithMany(t => t.{reverseNav.GetName()})");
                                        line.Append($".HasForeignKey(t => t.{fkColumnProp.GetName()})");
                                    }
                                    constructor.Add(line);
                                }
                            });

                            constructor.AddComment("Many to Many");
                            table.ManyToMany().ToList().ForEach(mtm =>
                            {
                                if (mtm.ForeignKeyColumn.PrimaryKeyOrder > 1)
                                {
                                    return;
                                }


                                var manyToManyTable = mtm.ForeignKeyColumn.Table;
                                var otherFk         = mtm.ForeignKeyColumn.Table.ForeignKeys.First(t => t.ForeignKeyColumn.PrimaryKeyOrder > 1);
                                var otherFkTable    = otherFk.PrimaryKeyColumn.Table;
                                var manyProp        = FindNavigation(entityClass, mtm);

                                // exclude if not being generated.
                                if (!TablesToGenerate.Contains(otherFk.PrimaryKeyColumn.Table))
                                {
                                    return;
                                }

                                var otherNamespace = TableNamespace(otherFkTable);
                                var otherClassName = TableClassName(otherFkTable);
                                var otherEntity    = GenerationContext.FindClass(otherClassName, otherNamespace);
                                var otherProp      = FindNavigation(otherEntity, otherFk);

                                var line = RawLineBuilder.Create();
                                line.Append($"HasMany(t => t.{manyProp.GetName()})");
                                line.Append($".WithMany(t => t.{otherProp.GetName()})");
                                line.Append($".Map(t => t.{ToTableFluent(manyToManyTable)}");
                                line.Append($".MapLeftKey(\"{mtm.ForeignKeyColumn.Name}\")");
                                line.Append($".MapRightKey(\"{otherFk.ForeignKeyColumn.Name}\"))");
                                constructor.Add(line);
                            });
                        });
                    });
                });
            });
        }