Table of Contents

Class ValueGenerator

Namespace
Microsoft.EntityFrameworkCore.ValueGeneration
Assembly
Microsoft.EntityFrameworkCore.dll

Generates values for properties when an entity is added to a context.

public abstract class ValueGenerator
Inheritance
ValueGenerator
Derived
Inherited Members

Remarks

See EF Core value generation for more information and examples.

Constructors

ValueGenerator()

protected ValueGenerator()

Properties

GeneratesStableValues

Gets a value indicating whether the values generated are stable. That is, the value will always be the same for a given property in a given entity, and does not depend on what other values may have been generated previously. For example, discriminator values generated for a TPH hierarchy are stable. Stable values will never be marked as unknown.

public virtual bool GeneratesStableValues { get; }

Property Value

bool

Remarks

See EF Core value generation for more information and examples.

GeneratesTemporaryValues

Gets a value indicating whether the values generated are temporary (i.e they should be replaced by database generated values when the entity is saved) or are permanent (i.e. the generated values should be saved to the database).

public abstract bool GeneratesTemporaryValues { get; }

Property Value

bool

Remarks

An example of temporary value generation is generating negative numbers for an integer primary key that are then replaced by positive numbers generated by the database when the entity is saved. An example of permanent value generation are client-generated values for a Guid primary key which are saved to the database.

See EF Core value generation for more information and examples.

Methods

Next(EntityEntry)

Gets a value to be assigned to a property.

public virtual object? Next(EntityEntry entry)

Parameters

entry EntityEntry

The change tracking entry of the entity for which the value is being generated.

Returns

object

The value to be assigned to a property.

Remarks

See EF Core value generation for more information and examples.

NextAsync(EntityEntry, CancellationToken)

Gets a value to be assigned to a property.

public virtual ValueTask<object?> NextAsync(EntityEntry entry, CancellationToken cancellationToken = default)

Parameters

entry EntityEntry

The change tracking entry of the entity for which the value is being generated.

cancellationToken CancellationToken

A CancellationToken to observe while waiting for the task to complete.

Returns

ValueTask<object>

The value to be assigned to a property.

Remarks

See EF Core value generation for more information and examples.

Exceptions

OperationCanceledException

If the CancellationToken is canceled.

NextValue(EntityEntry)

Template method to be overridden by implementations to perform value generation.

protected abstract object? NextValue(EntityEntry entry)

Parameters

entry EntityEntry

The change tracking entry of the entity for which the value is being generated.

Returns

object

The generated value.

Remarks

See EF Core value generation for more information and examples.

NextValueAsync(EntityEntry, CancellationToken)

Template method to be overridden by implementations to perform value generation.

protected virtual ValueTask<object?> NextValueAsync(EntityEntry entry, CancellationToken cancellationToken = default)

Parameters

entry EntityEntry

The change tracking entry of the entity for which the value is being generated.

cancellationToken CancellationToken

A CancellationToken to observe while waiting for the task to complete.

Returns

ValueTask<object>

The generated value.

Remarks

See EF Core value generation for more information and examples.

Exceptions

OperationCanceledException

If the CancellationToken is canceled.

WithConverter(ValueConverter)

Wraps this ValueGenerator such that it processes values converted with the given ValueConverter.

public virtual ValueGenerator WithConverter(ValueConverter converter)

Parameters

converter ValueConverter

The value converter.

Returns

ValueGenerator

A new value generator that works with the converted values.