Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HasDefaultValue() #829

Open
TimSirmovics opened this issue Jan 17, 2024 · 2 comments
Open

HasDefaultValue() #829

TimSirmovics opened this issue Jan 17, 2024 · 2 comments

Comments

@TimSirmovics
Copy link
Contributor

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
Copy link
Owner

sjh37 commented Jan 18, 2024

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

@sjh37
Copy link
Owner

sjh37 commented Jan 18, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants