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
706 stars 229 forks source link

How can I customize the *Configuration classes output? #141

Open rosdi opened 7 years ago

rosdi commented 7 years ago

I am trying to customize the output of the *Configuration classes..., basically I want all columns that are named CreatedDate to have DatabaseGeneratedOption.Computed annotation.

Right now I butchered the EF.Reverse.POCO.Core.ttinclude to have that effect, and it works, here is how I did it.

-- EF.Reverse.POCO.Core.ttinclude around line 806 --

private void SetupConfig()
{
  string databaseGeneratedOption = string.Empty;

  //-- my edit -- Make CreatedDate automatically assigned by SQL GETDATE() default value
  if(Name.Equals("CreatedDate", StringComparison.InvariantCultureIgnoreCase))
    databaseGeneratedOption = ".HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Computed)";
  //--end my edit 

  if(IsIdentity)
    databaseGeneratedOption = ".HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)";

And i get a nice output like this...

Property(x => x.CreatedDate).HasColumnName(@"CreatedDate").IsRequired().HasColumnType("datetime2").HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Computed);

But I have the feeling this is not the proper place to do this, if I upgrade Reverse POCO Generator, my changes will be overwritten.

I have browsed through the Database.tt script but could not figure out a way to achieve this in that file.

sjh37 commented 7 years ago

I would need to add a callback to allow extra config options to be set. I'll get back to this once data annotations are completed.

rosdi commented 7 years ago

Ok thanks.

rosdi commented 7 years ago

Duh!... I am an idiot... I could just use annotation to achieve the same purpose!

Func<Column, string> WritePocoColumn = c =>
{
    if (c.NameHumanCase == "CreatedDate") 
        return "System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedAttribute(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Computed)] " + c.Entity;

    return c.Entity;
};
ody-github commented 7 years ago

Hi, I'd like to output properties (Columns) with back-end fields instead of auto-properties, I'd like to add some logic in the setter.

For that I'd like to know how to get the column Type.

Is WritePocoColumn, the right place to output properties? Thanks.