Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the `DatabaseGeneratedOption.None` exist?

This is used with the Property(() => p).HasDatabaseGeneratedOption() call. Is is perhaps to turn off default DB value generation?

like image 832
ProfK Avatar asked Mar 22 '13 10:03

ProfK


2 Answers

EF uses DatabaseGeneratedOption to figure out what to do with the value of a key column for new entities. If the DatabaseGeneratedOption is Identity EF knows that the value the property is set to can be ignored and that the one that comes from the database should be used. If the DatabaseGeneratedOption is None EF will insert the value of the property to the database as the value of the key column.

In Code First - when Code First conventions find an int property that can be the key property for the given entity they by default will configure this column as identity column (meaning the database will generate the value of the key column/property). DatabaseGeneratedOption.None allows you to overwrite this if you want to set key values on your own.

like image 127
Pawel Avatar answered Nov 21 '22 11:11

Pawel


Its effect is to configure EF to not fetch a new identity value after inserting into the database.

like image 36
qujck Avatar answered Nov 21 '22 11:11

qujck