コード例 #1
0
        protected override void GenerateContext()
        {
            var contextNamespace = ContextNamespace();
            var contextClassName = ContextClassName();

            Action <FileBuilder> generateContextInline = (FileBuilder fileBuilder) =>
            {
                if (!Options.OutputToSingleFile)
                {
                    var filePath = $"{Options.OutputDir}{Path.DirectorySeparatorChar}{Options.ContextName}.generated.cs";
                    fileBuilder.Path(filePath);
                }

                fileBuilder.Using("Microsoft.EntityFrameworkCore");

                fileBuilder.Namespace(contextNamespace, true, ns =>
                {
                    ns.Class(contextClassName, true, contextClass =>
                    {
                        contextClass.Partial(true).Inherits(Options.ContextBaseClassName);

                        TablesToGenerate.ForEach(table =>
                        {
                            var tableClassFullName = TableClassFullName(table);
                            var tableNamePlural    = Pluralize(table.Name);
                            contextClass.Property(tableNamePlural, true, dbSetProp =>
                            {
                                dbSetProp.Virtual(true).Type($"DbSet<{tableClassFullName}>");
                            });
                        });

                        // empty constructor.
                        contextClass.Constructor(c => c.Class(contextClass));

                        // constructor with options.
                        contextClass.Constructor(c => c
                                                 .Class(contextClass)
                                                 .Parameter(p => p.Type($"DbContextOptions<{contextClassName}>").Name("options"))
                                                 .BaseParameter("options")
                                                 );

                        // override On Configuring
                        contextClass.Method(m =>
                        {
                            m
                            .AccessModifier(AccessModifiers.Protected)
                            .Override(true)
                            .ReturnType("void")
                            .Name("OnConfiguring")
                            .Parameter(p => p.Type("DbContextOptionsBuilder").Name("optionsBuilder"));

                            if (Options.AddConnectionStringOnGenerate)
                            {
                                m.Add(() =>
                                {
                                    return(IfBuilder.Create()
                                           .RawCondition(c => c.Condition("!optionsBuilder.IsConfigured"))
                                           .Add(RawLineBuilder.Create(
                                                    "#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.")
                                                .NoEndOfLine())
                                           .Add(UseDatabaseEngineConnectionStringLine()));
                                });
                            }
                        });

                        // model creating.
                        contextClass.Method(m =>
                        {
                            m
                            .AccessModifier(AccessModifiers.Protected)
                            .Override(true)
                            .ReturnType("void")
                            .Name("OnModelCreating")
                            .Parameter(p => p.Type("ModelBuilder").Name("modelBuilder"));

                            TablesToGenerate.ForEach(table =>
                            {
                                AddFluentToMethod(m, table);
                            });

                            SequenceToGenerate.ForEach(sequence =>
                            {
                                var dataType   = DataTypeResolver.ResolveType(sequence);
                                var outputType = dataType.GetOutputType();
                                m.RawLine($"modelBuilder.HasSequence<{outputType}>(\"{sequence.Name}\").StartsAt({sequence.StartAt}).IncrementsBy({sequence.IncrementsBy})");
                            });
                        });
                    });
                });
            };

            if (Options.OutputToSingleFile)
            {
                GenerationContext.SingleFile(fb => generateContextInline(fb));
            }
            else
            {
                GenerationContext.FileIfPathIsSet(fb => generateContextInline(fb));
            }
        }
コード例 #2
0
        protected virtual void AddFluentToMethod(MethodBuilder methodBuilder, ITable table)
        {
            var tableNamespace     = TableNamespace(table);
            var tableClassName     = TableClassName(table);
            var tableFullClassName = TableClassFullName(table);
            var tableClass         = GenerationContext.FindClass(tableClassName, tableNamespace);

            var fluentExpression = MultiLineLambdaExpression.Create()
                                   .Parameter(p => p.Name("entity"))
                                   .RawLine($"entity.{ToTableFluent(table)}");
            //.RawLine($"entity.ToTable(\"{table.Name}\", \"{table.Schema}\")");

            var pks             = table.Columns.Where(t => t.IsPrimaryKey);
            var hasCompositeKey = pks.Count() > 1;

            ; if (hasCompositeKey)
            {
                var def = string.Join(", ", pks.Select(pk =>
                {
                    var pkProp = tableClass.FindByMeta <PropertyBuilder>(pk);
                    return($"t.{pkProp.GetName()}");
                }));
                fluentExpression.RawLine($"entity.HasKey(t => new {{ {def} }})");
            }
            else
            {
                var pk     = pks.First();
                var pkProp = tableClass.FindByMeta <PropertyBuilder>(pk);
                fluentExpression.RawLine($"entity.HasKey(t => t.{pkProp.GetName()})");
            }

            table.Indexes.ForEach(i =>
            {
                if (!ShouldGenerateIndex(i))
                {
                    return;
                }

                var line = RawLineBuilder.Create();

                string rightExpr;
                if (i.Columns.Count == 1)
                {
                    var indexProp = tableClass.FindByMeta <PropertyBuilder>(i.Columns.First());
                    rightExpr     = $"t.{indexProp.GetName()}";
                }
                else
                {
                    var cols  = string.Join(", ", i.Columns.Select(t => $"t.{tableClass.FindByMeta<PropertyBuilder>(t).GetName()}"));
                    rightExpr = $"new {{ {cols} }}";
                }

                line.Append($"entity.HasIndex(t => {rightExpr})");
                line.Append($"\n\t.HasName(\"{i.Name}\")");
                if (i.IsUnique)
                {
                    line.Append("\n\t.IsUnique()");
                }

                OnBeforeIndexLineAdded(line, i);

                fluentExpression.Add(line);
            });

            table.Columns.ForEach(c =>
            {
                var columnProp = tableClass.FindByMeta <PropertyBuilder>(c);
                var line       = RawLineBuilder.Create();
                line.Append($"entity.Property(t => t.{columnProp.GetName()})");
                line.Append($".HasColumnType(\"{FluentColumnType(c)}\")");

                if (c.IsPrimaryKey)
                {
                    if (c.IsAutoIncrement)
                    {
                        line.Append(".ValueGeneratedOnAdd()");
                    }
                    else
                    {
                        line.Append(".ValueGeneratedNever()");
                    }
                }
                else if (!string.IsNullOrWhiteSpace(c.DefaultValue))
                {
                    line.Append($".HasDefaultValueSql(\"{c.DefaultValue}\")");
                }

                if (!c.IsNullable)
                {
                    line.Append(".IsRequired()");
                }

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

                if (DataTypeResolver.IsString(c) && !DataTypeResolver.IsUnicode(c))
                {
                    line.Append(".IsUnicode(false)");
                }

                fluentExpression.Add(line);
            });

            table.ForeignKeys.ForEach(fk =>
            {
                if (!TablesToGenerate.Contains(fk.PrimaryKeyColumn.Table))
                {
                    return;
                }

                var fkProp           = tableClass.FindByMeta <PropertyBuilder>(fk);
                var fkColumnProp     = tableClass.FindByMeta <PropertyBuilder>(fk.ForeignKeyColumn);
                var fkTableNamespace = TableNamespace(fk.PrimaryKeyColumn.Table);
                var fkTableClassName = TableClassName(fk.PrimaryKeyColumn.Table);
                var fkTableClass     = GenerationContext.FindClass(fkTableClassName, fkTableNamespace);
                var reverseProp      = fkTableClass.FindByMeta <PropertyBuilder>(fk);

                var line = RawLineBuilder.Create();

                line.Append($"entity.HasOne(t => t.{fkProp.GetName()})");

                if (!fk.IsOneToOne())
                {
                    line.Append($"\n\t.WithMany(t => t.{reverseProp.GetName()})");
                    line.Append($"\n\t.HasForeignKey(t => t.{fkColumnProp.GetName()})");
                }
                else
                {
                    line.Append($"\n\t.WithOne(t => t.{reverseProp.GetName()})");
                    line.Append($"\n\t.HasForeignKey<{tableFullClassName}>(t => t.{fkColumnProp.GetName()})");
                }

                if (IsCascade(fk.DeleteCascadeAction))
                {
                    line.Append("\n\t.OnDelete(DeleteBehavior.Delete)");
                }
                else if (IsSetNull(fk.DeleteCascadeAction))
                {
                    line.Append("\n\t.OnDelete(DeleteBehavior.SetNull)");
                }
                else
                {
                    line.Append("\n\t.OnDelete(DeleteBehavior.ClientSetNull)");
                }

                line.Append($"\n\t.HasConstraintName(\"{fk.Name}\")");

                line.Comment("Foreign Key");
                fluentExpression.Add(line);
            });


            var modelFluentLine = $"modelBuilder.Entity<{tableFullClassName}>({fluentExpression.GenerateInline()})";

            methodBuilder.Add(RawLineBuilder.Create(modelFluentLine));
            methodBuilder.AddEmptyLine();
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: PoweredSoft/CodeGenerator
        static void Main(string[] args)
        {
            var a = new
            {
                SomeInfo = "ABC"
            };

            var ctx = GenerationContext
                      .Create()
                      .File(file =>
            {
                file
                .Using("System")
                .Namespace(ns =>
                {
                    ns
                    .Name("Acme.Dal")
                    .Class(c =>
                    {
                        c.Attribute(attr => attr
                                    .Name("ModelAttribute")
                                    .Value(RawInlineBuilder.Create("\"Foo.Bar\""))
                                    .Value(RawInlineBuilder.Create("1"))
                                    );

                        c
                        .Name("Person")
                        .Partial(true)
                        .Property(p => p.Name("Id").Type("long").Virtual(true).Attribute(ab => ab.Name("SomeAttribute").Value("1", "\"David\"")))
                        .Property(p => p.Name("FirstName").Type("string").Virtual(true))
                        .Property(p => p.Name("LastName").Type("string").Virtual(true).Meta(a));
                    })
                    .Class(c =>
                    {
                        c
                        .Name("PersonModelBase")
                        .Partial(true)
                        .Property(p => p.Name("Id").Type("long?").Virtual(true))
                        .Property(p => p.Name("FirstName").Type("string").Virtual(true))
                        .Property(p => p.Name("LastName").Type("string").Virtual(true))
                        .Method(m =>
                        {
                            m
                            .Name("From")
                            .Virtual(true)
                            .Parameter(p => p.Name("entity").Type("Person"))
                            .Add(RawLineBuilder.Create("Id = entity.Id"))
                            .Add(RawLineBuilder.Create("FirstName = entity.FirstName"))
                            .Add(RawLineBuilder.Create("LastName = entity.LastName"));
                        })
                        .Method(m =>
                        {
                            m
                            .Attribute(ma => ma.Name("CoolMethod"))
                            .Attribute(ma => ma.Name("OtherMethod").Value("1", "false"))
                            .Name("To")
                            .Virtual(true)
                            .Parameter(p => p.Name("entity").Type("Person"))
                            .Add(() =>
                            {
                                return(IfBuilder
                                       .Create()
                                       .RawCondition(rc => rc.Condition("Id != null"))
                                       .Add(RawLineBuilder.Create("entity.Id = Id.Value")));
                            })
                            .Add(RawLineBuilder.Create("entity.FirstName = FirstName"))
                            .Add(RawLineBuilder.Create("entity.LastName = LastName"));
                        });
                    })
                    ;
                });
            });

            var propertyOfMetaA = ctx.FindClass("Person").FindByMeta <PropertyBuilder>(a);

            var lines = ctx.Files.First().GenerateLines();

            Console.WriteLine(string.Join("\n", lines));
            Console.ReadKey();


            /*
             * var ns = NamespaceBuilder
             *  .Create()
             *  .Name("Acme.Models")
             *  .Class(c => c
             *      .Partial(true)
             *      .Name("PersonModelBase")
             *      //.Field(f => f.AccessModifier(AccessModifiers.Private).Name("_firstName").Type("string"))
             *      //.Field(f => f.AccessModifier(AccessModifiers.Private).Name("_lastName").Type("string"))
             *      .Property(p => p.Name("FirstName").Type("string").Virtual(true))
             *      .Property(p => p.Name("LastName").Type("string").Virtual(true))
             *      .Property(p => p.Name("DateOfBirth").Type("DateTime?").Virtual(true))
             *      .Method(fromMethod =>
             *      {
             *          fromMethod
             *              .Name("From")
             *              .Virtual(true)
             *              .ReturnType("void")
             *              .Parameter(p => p.Name("entity").Type("Acme.Dal.Person"))
             *              .Add(() =>
             *              {
             *                  return IfBuilder
             *                      .Create()
             *                      .RawCondition(rc => rc.Condition("entity.DateOfBirth != null"))
             *                      .Add(RawLineBuilder.Create("DateOfBirth = entity.DateOfBirth"));
             *              })
             *              .Add(() =>
             *              {
             *                  return ElseIfBuilder
             *                      .Create()
             *                      .RawCondition(rc => rc.Condition("entity.DateOfBirth == null"))
             *                      .Add(RawLineBuilder.Create("DateOFBirth = DateTime.Now"));
             *              })
             *              .Add(() =>
             *              {
             *                  return ElseBuilder
             *                      .Create()
             *                      .Add(RawLineBuilder.Create("entity = null"));
             *              });
             *      })
             *  );
             *
             *
             * var lines = FileBuilder
             *  .Create()
             *  .Using("System")
             *  .Add(ns)
             *  .GenerateLines();
             *
             *  //.SaveToFile("C:\\test\\Person.cs", Encoding.UTF8);
             *
             * Console.WriteLine(string.Join("\n", lines));
             * Console.ReadKey();*/
        }
コード例 #4
0
ファイル: DatabaseGenerator.cs プロジェクト: dave1374/DbUtils
 protected override RawLineBuilder UseDatabaseEngineConnectionStringLine() => RawLineBuilder.Create($"optionsBuilder.UseSqlServer(\"{Options.ConnectionString}\")");
コード例 #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);
                            });
                        });
                    });
                });
            });
        }
コード例 #6
0
        protected virtual void GenerateModelExtensions(ITable table)
        {
            if (!Options.GenerateModelExtensions)
            {
                return;
            }

            if (!Options.GenerateModelsInterfaces && !Options.GenerateModels)
            {
                throw new Exception("Impossible to generate model extensions because neither GenerateModels or GenerateModelsInterfaces is activated.");
            }

            // full paths to model or interface
            var modelFullClassName     = ModelClassFullName(table);
            var modelInterfaceFullName = ModelInterfaceFullName(table);

            // poco.
            var pocoFullInterfaceName = TableInterfaceFullName(table);
            var pocoFullClassName     = TableClassFullName(table);

            // poco type.
            var pocoClassName      = TableClassName(table);
            var pocoClassNamespace = TableNamespace(table);
            var pocoClass          = GenerationContext.FindClass(pocoClassName, pocoClassNamespace);

            var modelExtensionNamespaceName = ModelExtensionsNamespace(table);
            var modelExtensionClassName     = ModelExtensionsClassName(table);
            var fileBuilder = ResolveModelExtensionFileBuilder(table);

            fileBuilder.Namespace(modelExtensionNamespaceName, true, ns =>
            {
                ns.Class(c =>
                {
                    // set basic info.
                    c.Partial(true).Static(true).Name(modelExtensionClassName);

                    var finalEntityType = Options.GenerateInterfaces ? pocoFullInterfaceName : pocoFullClassName;
                    var finalModelType  = Options.GenerateModelsInterfaces ? modelInterfaceFullName : modelFullClassName;

                    c.Method(m =>
                    {
                        m
                        .AccessModifier(AccessModifiers.Public)
                        .IsStatic(true)
                        .Name("ToModel")
                        .ReturnType("void")
                        .Parameter(p => p.Name("source").Type($"this {finalEntityType}"))
                        .Parameter(p => p.Name("model").Type(finalModelType));

                        table.Columns.ForEach(column =>
                        {
                            m.RawLine($"model.{column.Name} = source.{column.Name}");
                        });
                    });

                    c.Method(m =>
                    {
                        m
                        .AccessModifier(AccessModifiers.Public)
                        .IsStatic(true)
                        .Name("FromModel")
                        .ReturnType("void")
                        .Parameter(p => p.Name("model").Type($"this {finalModelType}"))
                        .Parameter(p => p.Name("destination").Type(finalEntityType))
                        .Parameter(p => p.Name("ignorePrimaryKey").Type("bool").DefaultValue("true"))
                        ;

                        table.Columns.ForEach(column =>
                        {
                            var rawLine             = "";
                            bool isPropertyNullable = IsModelPropertyNullable(column); // column.IsNullable || ShouldGenerateModelPropertyAsNullable(column);
                            if (isPropertyNullable && !column.IsNullable)
                            {
                                var matchingProp = pocoClass.FindByMeta <PropertyBuilder>(column);
                                var ternary      = TernaryBuilder
                                                   .Create()
                                                   .RawCondition(rc => rc.Condition($"model.{column.Name} != null"))
                                                   .True(RawInlineBuilder.Create(
                                                             $"destination.{column.Name} = ({matchingProp.GetTypeName()})model.{column.Name}"))
                                                   .False(RawInlineBuilder.Create(
                                                              $"destination.{column.Name} = default({matchingProp.GetTypeName()})"));

                                rawLine = $"destination.{column.Name} = {ternary.GenerateInline()}";
                            }
                            else
                            {
                                rawLine = $"destination.{column.Name} = model.{column.Name}";
                            }

                            if (column.IsPrimaryKey)
                            {
                                m.Add(IfBuilder.Create().RawCondition(rc => rc.Condition($"ignorePrimaryKey != true")).Add(RawLineBuilder.Create(rawLine)));
                            }
                            else
                            {
                                m.RawLine(rawLine);
                            }
                        });
                    });
                });
            });
        }