dotnet / EntityFramework.Docs

Documentation for Entity Framework Core and Entity Framework 6
https://docs.microsoft.com/ef/
Creative Commons Attribution 4.0 International
1.63k stars 1.96k forks source link

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

Open idchlife opened 4 years ago

idchlife commented 4 years ago

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.

yehiasalam commented 4 years ago

yes exactly, its mentioned in ef6 docs here https://docs.microsoft.com/en-us/ef/ef6/saving/change-tracking/property-values#getting-and-setting-the-current-or-original-value-of-an-individual-property but same example is not working in efcore