sjh37 / EntityFramework-Reverse-POCO-Code-First-Generator

EntityFramework Reverse POCO Code First Generator - Beautifully generated code that is fully customisable. This generator creates code as if you reverse engineered a database and lovingly created the code by hand. It is free to academics (you need a .edu or a .ac email address), not free for commercial use. Obtain your licence from
https://www.reversepoco.co.uk/
Other
700 stars 230 forks source link

WritePocoColumn not available when upgrading to V3 #776

Closed joeldroid closed 1 year ago

joeldroid commented 1 year ago

Right now we are using v2 and we are extending WritePocoColumn(Func<Column, string> WritePocoColumn = c =>) function to add some custom column types into the generated classes. We are using DB first code generation, and I was wondering if there is a way to add/override this method in V3?

Any help on this is appreciated.

sjh37 commented 1 year ago

Hi @joeldroid

Much easier V3. Update the Settings.UpdateTable callback like so:

// Use the following function if you need to apply additional modifications to a table
// Called just before UpdateColumn
Settings.UpdateTable = delegate(Table table)
{
    // Add an extra comment
    //if (table.NameHumanCase.Equals("SomeTable", StringComparison.InvariantCultureIgnoreCase))
    //    table.AdditionalComment = "Example comment";

    // To add a base class to a table based on column names. Also see Settings.UpdateColumn below.
    //var tracking  = new List<string> { "createdby", "createdon", "modifiedby", "modifiedon" };
    //var replicate = new List<string> { "uniqueid", "hrid"};
    //if (table.Columns.Any(x => tracking.Contains(x.NameHumanCase)))
    //    table.BaseClasses = " : TrackingEntity";
    //if (table.Columns.Any(x => replicate.Contains(x.NameHumanCase)))
    //    table.BaseClasses = " : ReplicateEntity";

    if (!table.IsView)
    {
        table.Columns.Add(new Column
        {
            DbName = "column_name_in_db",
            NameHumanCase = "ColumnNameInCSharp",
            PropertyType = "int",
            SqlPropertyType = "INT",
            IsNullable = false,
            IsUnicode = true,
            Default = string.Empty,
            Ordinal = 999 // Places the column at the bottom
            // Any other settings for the column can be added here
        });
    }

    // To add attributes
    //table.Attributes.Add("[Serializable]");
};

That produces entities that look like this:

// Categories
public class Category
{
    public int CategoryId { get; set; } // CategoryID (Primary key)
    public string CategoryName { get; set; } // CategoryName (length: 15)
    public string Description { get; set; } // Description (length: 1073741823)
    public byte[] Picture { get; set; } // Picture (length: 2147483647)
    public int ColumnNameInCSharp { get; set; } // column_name_in_db

    // Reverse navigation

    /// <summary>
    /// Child Products where [Products].[CategoryID] point to this entity (FK_Products_Categories)
    /// </summary>
    public virtual ICollection<Product> Products { get; set; } // Products.FK_Products_Categories

    public Category()
    {
        Products = new List<Product>();
    }
}

And are configured like this:

// Categories
public class CategoryConfiguration : IEntityTypeConfiguration<Category>
{
    public void Configure(EntityTypeBuilder<Category> builder)
    {
        builder.ToTable("Categories", "dbo");
        builder.HasKey(x => x.CategoryId).HasName("PK_Categories").IsClustered();

        builder.Property(x => x.CategoryId).HasColumnName(@"CategoryID").HasColumnType("int").IsRequired().ValueGeneratedOnAdd().UseIdentityColumn();
        builder.Property(x => x.CategoryName).HasColumnName(@"CategoryName").HasColumnType("nvarchar(15)").IsRequired().HasMaxLength(15);
        builder.Property(x => x.Description).HasColumnName(@"Description").HasColumnType("ntext").IsRequired(false);
        builder.Property(x => x.Picture).HasColumnName(@"Picture").HasColumnType("image(2147483647)").IsRequired(false).HasMaxLength(2147483647);
        builder.Property(x => x.ColumnNameInCSharp).HasColumnName(@"column_name_in_db").HasColumnType("INT").IsRequired();

        builder.HasIndex(x => x.CategoryName).HasDatabaseName("CategoryName");
    }
}
joeldroid commented 1 year ago

Thanks @sjh37 I will try implementing that.