Skip to main content
Version: 0.3.x (0.3.0)

Class ValueObjectInt32KeyGenerationExtensions

Namespace: AdCodicem.ValueObjects.EntityFrameworkCore
Assembly: AdCodicem.ValueObjects.EntityFrameworkCore.dll

Generates a key over a 32-bit integer the way the provider generates an key.

public static class ValueObjectInt32KeyGenerationExtensions

Inheritance​

object ← ValueObjectInt32KeyGenerationExtensions

Inherited Members​

object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.MemberwiseClone(), object.ReferenceEquals(object?, object?), object.ToString()

Methods​

HasValueObjectKeyGeneration<TSelf>(PropertyBuilder<TSelf>)​

Has the database generate the key when an entity is added: an identity column on SQL Server and PostgreSQL, the row identifier on SQLite, with a temporary value in the change tracker until the row is saved.

public static PropertyBuilder<TSelf> HasValueObjectKeyGeneration<TSelf>(this PropertyBuilder<TSelf> builder) where TSelf : struct, IValueObject<TSelf, int>

Parameters​

builder PropertyBuilder<TSelf>

Property builder of the key.

Returns​

PropertyBuilder<TSelf>

The same builder, so calls can be chained.

Type Parameters​

TSelf

Value object type, over .

Remarks​

The key becomes generated on add, as an key is by convention, and gets the generator of , which a compiled model can write. The column becomes an IDENTITY on SQL Server, a GENERATED BY DEFAULT AS IDENTITY on PostgreSQL, and stays an INTEGER PRIMARY KEY, the row identifier, on SQLite, which reuses the highest key once it is deleted unless the property also calls SQLite's UseAutoincrement(): unlike a key of the itself, which is AUTOINCREMENT there by convention.

Until SaveChanges, the change tracker holds a temporary value, negative and never written, and the entity's property its default; the key the database generates then takes its place, read through the converter, so a type counted from one accepts it, in a strict context too. Zero, the default, is generated over; any other key the domain set is kept, which SQL Server then refuses to insert into an identity column, and PostgreSQL stores. Only a relational database generates the key: on another provider, Entity Framework Core's in-memory database for one, adding an entity without its key throws an , rather than store the temporary value as the key.

SQL Server cannot alter an existing column into an identity column: a migration from a model where the key was not generated fails, and the table has to be created again. On PostgreSQL, such a migration starts the identity at one, below the keys the table already holds.