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

HasDefaultValue() #829

Open TimSirmovics opened 5 months ago

TimSirmovics commented 5 months ago

When a column has a default value, this is implemented by setting that value in the constructor of the entity.

Is it possible to also generate the .HasDefaultValue() in the entity configuration class as per https://learn.microsoft.com/en-us/ef/core/modeling/generated-properties?tabs=data-annotations ?

Would this introduce any side effects?

The reason I ask is that I am using reflection to query the entity model and need to get the default value, but it is not represented in the metadata as it is only included inside the constructor.

sjh37 commented 5 months ago

Hi Tim, I think it will be ok. https://stackoverflow.com/a/53042416/5884

sjh37 commented 5 months ago

Some of it could be tricky.

.HasDefaultValueSql("getdate()");

Would convert to DateTime.Now(), however, how would I convert this to HasDefaultValue() ?

.HasDefaultValueSql("[LastName] + ', ' + [FirstName]");

If it is a simple integer, or getdate(), or something that is recognised as not being a complicated string, it's doable. But I fear it will not be a 100% foolproof solution.