dotnet / dotnet/EntityFramework.Docs

It's not clear how to generate value on the side of EF Core.

Open
#2,059 1 comment 0 reactions 0 assignees View on GitHub
area-value-generation consider-for-next-release
Dominant language
Mermaid
Stars
1.7k
Forks
2k
Avg merge
7d 23h
Merged PRs (30d)
16

Description

As it states on this page: A generated property is a property whose value is generated (either by EF or the database) when the entity is added and/or updated.

But also there is no example how to generate value on the side of EF Core. Maybe via lambda/arrow function or ValueGenerator object or something.

EF Core available for different databases via variety of providers.

So every database has their own generation strategies and implementations.

What if we want to generate value explicitly on the side of EF Core? .ValueGeneratedOnAdd()/ValueGeneratedOnAddOrUpdate() could be helpful if there would be ability to generate value in EF Core, and not only in SQL.

Quick example, HasDefaultValueSql("CURRENT_TIMESTAMP") working nicely for Postgres, but HasComputedValueSql("CURRENT_TIMESTAMP") is not for Postgres versions < 12 (theoretically, did not try >= 12 version yet), because of syntax errors. Meaning that there is no way to easily generate something like UpdatedAt columns in Postgres using EF Core, without manually overriding SaveChanges or SaveChangesAsync, which makes the whole situation kinda messy.

I suspect documentation lacks the crucial examples how to generate value on the side of EF Core if one potentially to create and update entities only using ORM.

It may be stupid, but what if we would want to have value generator like "Fill column differently for each one of 24 hours of day" for insert/updates, without using database prepared code for such generation on insert/update?

---
#### Document Details

⚠ *Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.*

* ID: e621a525-7104-74bf-5a17-9f5761f75fab
* Version Independent ID: 3d0e675b-3b2a-ebd9-273b-312c5c6ac092
* Content: [Setting Explicit Values for Generated Properties - EF Core](https://docs.microsoft.com/en-us/ef/core/saving/explicit-values-generated-properties#feedback)
* Content Source: [entity-framework/core/saving/explicit-values-generated-properties.md](https://github.com/aspnet/EntityFramework.Docs/blob/master/entity-framework/core/saving/explicit-values-generated-properties.md)
* Product: **entity-framework**
* Technology: **entity-framework-core**
* GitHub Login: @rowanmiller
* Microsoft Alias: **avickers**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.