What is the point of marking a property as "Value generated on add or update" in Entity Framework?

c# entity-framework entity-framework-core

Question

The Microsoft docs for Entity Framework show that you can mark a property of a type as "Value generated on add or update". So in the situation where I want a LastUpdated column to be given the current date when a row is updated, I can mark that property like so:

enter image description here

But as you can see, it gives a warning that EF isn't going to actually make it happen. Following that link in the warning explicitly tells us that if we want a Date column set to the current date whenever a row is updated, we need to go and create the trigger in the database ourselves to make it happen.

So what does it mean, and what is the point of "letting EF know that values are generated for added or updated entities"?

1
1
3/6/2018 11:49:15 AM

Accepted Answer

Well, "does not guarantee" bit is indeed a little vague.

But what you do want is for EF to understand that it should include these columns in a Select but not in an Update or Insert.

0
3/6/2018 11:37:49 AM

Popular Answer

You are reading it incorrectly. It says

It does not guarantee that EF will...

It doesn't say "EF will not do anything with this". If, for example, you are working on Code First, and you set up the PK to

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }

EF will actually set up that column with Identity (on SQL Server).

It also means that EF will not send that value on updates and inserts, since the database is supposed to fill those in.

Remember that EF works with many providers, and it's up to each provider to give that possibility, that's why it's not guaranteed.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow