EntityFramework-Reverse-POCO-Code-First-Generator icon indicating copy to clipboard operation
EntityFramework-Reverse-POCO-Code-First-Generator copied to clipboard

HasDefaultValue()

Open TimSirmovics opened this issue 1 year ago • 2 comments

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.

TimSirmovics avatar Jan 17 '24 22:01 TimSirmovics

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

sjh37 avatar Jan 18 '24 09:01 sjh37

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.

sjh37 avatar Jan 18 '24 09:01 sjh37