Why does the `DatabaseGeneratedOption.None` exist?

后端 未结 2 1111
星月不相逢
星月不相逢 2021-02-13 12:53

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

相关标签:
2条回答
  • 2021-02-13 12:56

    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.

    0 讨论(0)
  • 2021-02-13 13:05

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

    0 讨论(0)
提交回复
热议问题