Skip to content

Commit

Permalink
📝 Clarify value generation may require setup
Browse files Browse the repository at this point in the history
Per discussion on
https://github.com/aspnet/EntityFramework/issues/39551, adding some
notes to clarify that just marking a property as generated is not always
enough to actually get values generated.
  • Loading branch information
rowanmiller committed Dec 28, 2015
1 parent 416ae04 commit f5cf0d2
Showing 1 changed file with 35 additions and 13 deletions.
48 changes: 35 additions & 13 deletions docs/modeling/generated-properties.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ No value generation means that you will always supply a valid value to be saved
Value generated on add
^^^^^^^^^^^^^^^^^^^^^^

Value generated on add means that if you don't specify a value, one will be generated for you.
Value generated on add means that a value is generated for new entities.

.. caution::
How the value is generated for added entities will depend on the database provider being used. Database providers may automatically setup value generation for some property types, but others may require you to manually setup how the value is generated.

For example, when using SQL Server, values will be automatically generated for `GUID` properties (using the SQL Server sequential GUID algorithm). However, if you specify that a `DateTime` property is generated on add, then you must setup a way for the values to be generated (such as setting default value SQL of `GETDATE()`, see :doc:`relational/default-values`).

If you add an entity to the context that has a value assigned to the primary key property, then EF will attempt to insert that value rather than generating a new one. A property is considered to have a value assigned if it is not assigned the CLR default value (``null`` for ``string``, ``0`` for ``int``, ``Guid.Empty`` for ``Guid``, etc.).

Expand All @@ -28,6 +33,11 @@ Value generated on add or update

Value generated on add or update means that a new value is generated every time the record is saved (insert or update).

.. caution::
How the value is generated for added and updated entities will depend on the database provider being used. Database providers may automatically setup value generation for some property types, while others will require you to manually setup how the value is generated.

For example, when using SQL Server, `byte[]` properties that are set as generated on add or update and marked as concurrency tokens, will be setup with the `rowversion` data type - so that values will be generated in the database. However, if you specify that a `DateTime` property is generated on add or update, then you must setup a way for the values to be generated (such as a database trigger).

Like 'value generated on add', if you specify a value for the property on a newly added instance of an entity, that value will be inserted rather than a value being generated. Also, if you explicitly change the value assigned to the property (thus marking it as modified) then that new value will be set in the database rather than a value being generated.

Conventions
Expand All @@ -38,61 +48,73 @@ By convention, primary keys that are of an integer or GUID data type will be set
Data Annotations
----------------

No value generation
^^^^^^^^^^^^^^^^^^^
No value generation (Data Annotations)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. literalinclude:: configuring/sample/EFModeling.Configuring.DataAnnotations/Samples/ValueGeneratedNever.cs
:language: c#
:lines: 11-16
:emphasize-lines: 3
:linenos:

Value generated on add
^^^^^^^^^^^^^^^^^^^^^^
Value generated on add (Data Annotations)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. literalinclude:: configuring/sample/EFModeling.Configuring.DataAnnotations/Samples/ValueGeneratedOnAdd.cs
:language: c#
:lines: 12-18
:emphasize-lines: 5
:linenos:

Value generated on add or update
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. caution::
This just lets EF know that values are generated for added entities, it does not guarantee that EF will setup the actual mechanism to generate values. See `Value generated on add`_ section for more details.

Value generated on add or update (Data Annotations)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. literalinclude:: configuring/sample/EFModeling.Configuring.DataAnnotations/Samples/ValueGeneratedOnAddOrUpdate.cs
:language: c#
:lines: 12-18
:emphasize-lines: 5
:linenos:

.. caution::
This just lets EF know that values are generated for added or updated entities, it does not guarantee that EF will setup the actual mechanism to generate values. See `Value generated on add or update`_ section for more details.

Fluent API
----------

You can use the Fluent API to change the value generation pattern for a given property.

No value generation
^^^^^^^^^^^^^^^^^^^
No value generation (Fluent API)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. literalinclude:: configuring/sample/EFModeling.Configuring.FluentAPI/Samples/ValueGeneratedNever.cs
:language: c#
:lines: 5-21
:emphasize-lines: 7-9
:linenos:

Value generated on add
^^^^^^^^^^^^^^^^^^^^^^
Value generated on add (Fluent API)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. literalinclude:: configuring/sample/EFModeling.Configuring.FluentAPI/Samples/ValueGeneratedOnAdd.cs
:language: c#
:lines: 6-23
:emphasize-lines: 7-9
:linenos:

Value generated on add or update
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. caution::
This just lets EF know that values are generated for added entities, it does not guarantee that EF will setup the actual mechanism to generate values. See `Value generated on add`_ section for more details.

Value generated on add or update (Fluent API)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. literalinclude:: configuring/sample/EFModeling.Configuring.FluentAPI/Samples/ValueGeneratedOnAddOrUpdate.cs
:language: c#
:lines: 6-23
:emphasize-lines: 7-9
:linenos:

.. caution::
This just lets EF know that values are generated for added or updated entities, it does not guarantee that EF will setup the actual mechanism to generate values. See `Value generated on add or update`_ section for more details.

0 comments on commit f5cf0d2

Please sign in to comment.