Marking a field with the annotation specifies that a value will be automatically generated for that field. This is primarily intended for primary key fields but ObjectDB also supports this annotation for non-key numeric persistent fields as well. Several different value generation strategies can be used as explained below.
Entity Framework Core supports composite keys - primary key values generated from two or more fields in the database. However, unlike in previous versions of Entity Framework, you cannot use the Key attribute to define composite keys. This can only be done via the Fluent API. The Fluent API equivalent to the Key attribute is the. Gets or sets a value that indicates whether UI should be generated automatically in order to display this field. DisplayAttribute.AutoGenerateField Property (System.ComponentModel.DataAnnotations).
This page covers the following topics:
The Auto Strategy
ObjectDB maintains a special global number generator for every database. This number generator is used to generate automatic object IDs for entity objects with no primary key fields defined (as explained in the previous section).
The same number generator is also used to generate numeric values for primary key fields annotated by with the strategy:
is the default strategy, so the following definition is equivalent:
During a commit the strategy uses the global number generator to generate a primary key for every new entity object. These generated values are unique at the database level and are never recycled, as explained in the previous section.
The Identity Strategy
The strategy is very similar to the strategy:
Key Data Annotation Auto Generated Online
The strategy also generates an automatic value during commit for every new entity object. The difference is that a separate identity generator is managed per type hierarchy, so generated values are unique only per type hierarchy.
The Sequence Strategy
C# Data Annotations Key
The sequence strategy consists of two parts - defining a named sequence and using the named sequence in one or more fields in one or more classes. The annotation is used to define a sequence and accepts a name, an initial value (the default is 1) and an allocation size (the default is 50). A sequence is global to the application and can be used by one or more fields in one or more classes. The strategy is used in the annotation to attach the given field to the previously defined named sequence:
Unlike and , the strategy generates an automatic value as soon as a new entity object is persisted (i.e. before commit). This may be useful when the primary key value is needed earlier. To minimize round trips to the database server, IDs are allocated in groups. The number of IDs in each allocation is specified by the attribute. It is possible that some of the IDs in a given allocation will not be used. Therefore, this strategy does not guarantee there will be no gaps in sequence values.
Data Annotation Jobs
The Table Strategy
Data Annotations Key
The strategy is very similar to the strategy:
ORM-based JPA providers (such as Hibernate, TopLink, EclipseLink, OpenJPA, JPOX, etc.) simulate a sequence using a table to support this strategy. ObjectDB does not have tables, so the and strategies are almost identical.
Key Data Annotation Auto Generated Download
A tiny difference is related to the initial value attribute. Whereas the strategy maintains the next sequence number to be used the strategy maintains the last value that was used. The implication for the initialValue
attribute is that if you want sequence numbers to start with 1
in the strategy initialValue=0
has to be specified in the annotation.