Skip to main content

Class EntityIdConventionExtensions

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

Maps entity identifiers onto the narrowest column that can hold them.

public static class EntityIdConventionExtensions

Inheritance

objectEntityIdConventionExtensions

Inherited Members

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

Remarks

An identifier is a value object, so ConfigureValueObjects already maps it. What this adds is everything that follows from the identifier being fixed-width and ASCII: char(n) rather than varchar(n), non-Unicode so a SQL Server column is not silently doubled to nchar, and optionally a binary collation.

It also applies the conversion itself, so a model holding nothing but identifiers needs this call alone. Calling both is fine: the second call configures the same properties the same way.

What it deliberately does not do is decide the physical layout of your tables. On SQL Server a primary key is clustered by default, which makes the table itself order by the key; declaring it IsClustered(false) confines index churn to the ~30-byte index instead of the whole row, and with the monotonic time bucket at the head of the body the fill factor can go back up towards 95. Those are choices about your schema, not about the identifier type, and they need the provider-specific packages.

Methods

ConfigureEntityIds(ModelConfigurationBuilder, params Assembly[])

Maps every registered entity identifier to a fixed-width column.

public static ModelConfigurationBuilder ConfigureEntityIds(this ModelConfigurationBuilder builder, params Assembly[] assemblies)

Parameters

builder ModelConfigurationBuilder

Model configuration builder, from DbContext.ConfigureConventions.

assemblies Assembly[]

Assemblies declaring the identifiers. When none is given, everything already registered is mapped, which is enough as soon as the entity assembly has been loaded.

Returns

ModelConfigurationBuilder

The same builder, so calls can be chained.

ConfigureEntityIds(ModelConfigurationBuilder, string?, params Assembly[])

Maps every registered entity identifier to a fixed-width column with an explicit collation.

public static ModelConfigurationBuilder ConfigureEntityIds(this ModelConfigurationBuilder builder, string? collation, params Assembly[] assemblies)

Parameters

builder ModelConfigurationBuilder

Model configuration builder, from DbContext.ConfigureConventions.

collation string?

Collation for the identifier columns, or null to leave the database default in place. names the binary one per provider.

assemblies Assembly[]

Assemblies declaring the identifiers.

Returns

ModelConfigurationBuilder

The same builder, so calls can be chained.

Examples

protected override void ConfigureConventions(ModelConfigurationBuilder builder)
=> builder.ConfigureEntityIds(IdCollations.PostgreSql, typeof(AccountId).Assembly);

HasEntityIdConversion<TId>(PropertyBuilder<TId>, string?)

Maps a single property holding an entity identifier.

public static PropertyBuilder<TId> HasEntityIdConversion<TId>(this PropertyBuilder<TId> builder, string? collation = null) where TId : struct, IEntityId<TId>

Parameters

builder PropertyBuilder<TId>

Property builder.

collation string?

Collation for the column, or null for the database default.

Returns

PropertyBuilder<TId>

The same builder, so calls can be chained.

Type Parameters

TId

Identifier type.

Remarks

Use this for a property that needs to depart from the convention; otherwise prefer the convention.