This chapter takes you through the relationships between Entities. Generally the relations are more effective between tables in the database. Here the entity classes are treated as relational tables (concept of JPA), therefore the relationships between Entity classes are as follows: Many-To-One.
-->
A key serves as a unique identifier for each entity instance. Most entities in EF have a single key, which maps to the concept of a primary key in relational databases (for entities without keys, see Keyless entities). Entities can have additional keys beyond the primary key (see Alternate Keys for more information).
By convention, a property named Id
or <type name>Id
will be configured as the primary key of an entity.
Note
Owned entity types use different rules to define keys.
You can configure a single property to be the primary key of an entity as follows:
You can also configure multiple properties to be the key of an entity - this is known as a composite key. Composite keys can only be configured using the Fluent API; conventions will never setup a composite key, and you can not use Data Annotations to configure one.
By convention, on relational databases primary keys are created with the name PK_<type name>
. You can configure the name of the primary key constraint as follows:
While EF Core supports using properties of any primitive type as the primary key, including string
, Guid
, byte[]
and others, not all databases support all types as keys. In some cases the key values can be converted to a supported type automatically, otherwise the conversion should be specified manually.
Key properties must always have a non-default value when adding a new entity to the context, but some types will be generated by the database. In that case EF will try to generate a temporary value when the entity is added for tracking purposes. After SaveChanges is called the temporary value will be replaced by the value generated by the database. Openssl generate rsa private key.
Important
If a key property has its value generated by the database and a non-default value is specified when an entity is added, then EF will assume that the entity already exists in the database and will try to update it instead of inserting a new one. To avoid this turn off value generation or see how to specify explicit values for generated properties.
An alternate key serves as an alternate unique identifier for each entity instance in addition to the primary key; it can be used as the target of a relationship. When using a relational database this maps to the concept of a unique index/constraint on the alternate key column(s) and one or more foreign key constraints that reference the column(s).
Tip
If you just want to enforce uniqueness on a column, define a unique index rather than an alternate key (see Indexes). In EF, alternate keys are read-only and provide additional semantics over unique indexes because they can be used as the target of a foreign key.
Alternate keys are typically introduced for you when needed and you do not need to manually configure them. By convention, an alternate key is introduced for you when you identify a property which isn't the primary key as the target of a relationship.
You can also configure a single property to be an alternate key:
You can also configure multiple properties to be an alternate key (known as a composite alternate key):
Finally, by convention, the index and constraint that are introduced for an alternate key will be named AK_<type name>_<property name>
(for composite alternate keys <property name>
becomes an underscore separated list of property names). You can configure the name of the alternate key's index and unique constraint:
The establishment of production and to fill up the next-generation racing experience necessary to speed brings back the popular racing franchise complex customization, the city 's car culture, nocturnal in an open world and new narrative. Combine all of the five to create that perfect moment of Need for Speed; insanely hard driving, sliding through a corner on a customized ride together with your friends, while being chased by the cops. The game includes five ways to play: speed, style, build, crew, and a ban.
Package | Description |
---|---|
javax.persistence |
Java Persistence is the API for the management for persistence and object/relational mapping.
|
javax.persistence.criteria | |
javax.persistence.spi |
SPI for Java Persistence providers
|
Class | Description |
---|---|
AccessType |
Used with the
Access annotation to specify an access type to be applied to an entity class, mapped superclass, or embeddable class, or to a specific attribute of such a class. |
AssociationOverride |
Used to override a mapping for an entity relationship.
|
AttributeNode | |
AttributeOverride |
Used to override the mapping of a
Basic (whether explicit or default) property or field or Id property or field. |
Cache |
Interface used to interact with the second-level cache.
|
CacheRetrieveMode |
Used as the value of the
javax.persistence.cache.retrieveMode property to specify the behavior when data is retrieved by the find methods and by queries. |
CacheStoreMode |
Used as the value of the
javax.persistence.cache.storeMode property to specify the behavior when data is read from the database and when data is committed into the database. |
CascadeType |
Defines the set of cascadable operations that are propagated to the associated entity.
|
Column |
Specifies the mapped column for a persistent property or field.
|
ColumnResult |
Used in conjunction with the
SqlResultSetMapping annotation or ConstructorResult annotation to map a column of the SELECT list of a SQL query. |
ConstraintMode | |
ConstructorResult |
Used in conjunction with the
SqlResultSetMapping annotation to map the SELECT clause of a SQL query to a constructor. |
Convert |
Specifies the conversion of a Basic field or property.
|
DiscriminatorType |
Defines supported types of the discriminator column.
|
EntityGraph |
This type represents the root of an entity graph that will be used as a template to define the attribute nodes and boundaries of a graph of entities and entity relationships.
|
EntityManager |
Interface used to interact with the persistence context.
|
EntityManagerFactory |
Interface used to interact with the entity manager factory for the persistence unit.
|
EntityResult |
Used in conjunction with the
SqlResultSetMapping annotation to map the SELECT clause of a SQL query to an entity result. |
EntityTransaction |
Interface used to control transactions on resource-local entity managers.
|
EnumType | |
FetchType |
Defines strategies for fetching data from the database.
|
FieldResult |
Used in conjunction with the
EntityResult annotation to map columns specified in the SELECT list of a SQL query to the properties or fields of an entity class. |
FlushModeType | |
ForeignKey |
Used to specify the handling of foreign key constraints when schema generation is in effect.
|
GenerationType |
Defines the types of primary key generation strategies.
|
Index |
Used in schema generation to specify creation of an index.
|
InheritanceType | |
JoinColumn |
Specifies a column for joining an entity association or element collection.
|
JoinTable | |
LockModeType |
Lock modes can be specified by means of passing a
LockModeType argument to one of the EntityManager methods that take locks (lock , find , or refresh ) or to the Query.setLockMode() or TypedQuery.setLockMode() method. |
MapKeyJoinColumn |
Specifies a mapping to an entity that is a map key.
|
NamedAttributeNode |
A
NamedAttributeNode is a member element of a NamedEntityGraph . |
NamedEntityGraph |
Used to specify the path and boundaries for a find operation or query.
|
NamedNativeQuery | |
NamedQuery |
Specifies a static, named query in the Java Persistence query language.
|
NamedStoredProcedureQuery |
Specifies and names a stored procedure, its parameters, and its result type.
|
NamedSubgraph |
A
NamedSubgraph is a member element of a NamedEntityGraph . |
Parameter | |
ParameterMode |
Specifies the mode of a parameter of a stored procedure query.
|
PersistenceContext |
Expresses a dependency on a container-managed
EntityManager and its associated persistence context. |
PersistenceContextType |
Specifies whether a transaction-scoped or extended persistence context is to be used in
PersistenceContext . |
PersistenceException |
Thrown by the persistence provider when a problem occurs.
|
PersistenceProperty |
Describes a single container or persistence provider property.
|
PersistenceUnit |
Expresses a dependency on an
EntityManagerFactory and its associated persistence unit. |
PersistenceUnitUtil |
Utility interface between the application and the persistence provider managing the persistence unit.
|
PersistenceUtil |
Utility interface between the application and the persistence provider(s).
|
PessimisticLockScope |
Defines the values of the
javax.persistence.lock.scope property for pessimistic locking. |
PrimaryKeyJoinColumn |
Specifies a primary key column that is used as a foreign key to join to another table.
|
Query | |
QueryHint |
Used to supply a query property or hint to the
NamedQuery or NamedNativeQuery annotation. |
SecondaryTable |
Specifies a secondary table for the annotated entity class.
|
SequenceGenerator |
Defines a primary key generator that may be referenced by name when a generator element is specified for the
GeneratedValue annotation. |
SharedCacheMode |
Specifies how the provider must use a second-level cache for the persistence unit.
|
SqlResultSetMapping |
Specifies the mapping of the result of a native SQL query or stored procedure.
|
StoredProcedureParameter |
Specifies a parameter of a named stored procedure query.
|
StoredProcedureQuery |
Interface used to control stored procedure query execution.
|
Subgraph |
This type represents a subgraph for an attribute node that corresponds to a Managed Type.
|
SynchronizationType |
Specifies whether the persistence context is always automatically synchronized with the current transaction or whether the persistence context must be explicitly joined to the current transaction by means of the
EntityManager.joinTransaction() method. |
TableGenerator |
Defines a primary key generator that may be referenced by name when a generator element is specified for the
GeneratedValue annotation. |
TemporalType |
Type used to indicate a specific mapping of
java.util.Date or java.util.Calendar . |
TupleElement |
The
TupleElement interface defines an element that is returned in a query result tuple. |
TypedQuery |
Interface used to control the execution of typed queries.
|
UniqueConstraint |
Specifies that a unique constraint is to be included in the generated DDL for a primary or secondary table.
|
ValidationMode |
The validation mode to be used by the provider for the persistence unit.
|
Class | Description |
---|---|
Parameter | |
Tuple |
Interface for extracting the elements of a query result tuple.
|
TupleElement |
The
TupleElement interface defines an element that is returned in a query result tuple. |
Class | Description |
---|---|
EntityManagerFactory |
Interface used to interact with the entity manager factory for the persistence unit.
|
SharedCacheMode |
Specifies how the provider must use a second-level cache for the persistence unit.
|
ValidationMode |
The validation mode to be used by the provider for the persistence unit.
|