Class DatabaseScenarioBuilder<TEntryPoint, TDbContext>

Namespace
XBullet.EasyTesting.EntityFrameworkCore
Assembly
XBullet.EasyTesting.EntityFrameworkCore.dll

Fluently arranges one scoped EF Core database scenario.

public sealed class DatabaseScenarioBuilder<TEntryPoint, TDbContext> where TEntryPoint : class where TDbContext : DbContext

Type Parameters

TEntryPoint

The application entry-point type hosted by the test factory.

TDbContext

The EF Core context type used by the application.

Inheritance
DatabaseScenarioBuilder<TEntryPoint, TDbContext>
Inherited Members

Remarks

This mutable builder is not thread-safe and can execute only once. Arranged callbacks execute sequentially against one factory-owned scoped context and must not retain or dispose it.

Methods

Apply(Action<TDbContext>)

Adds a synchronous context mutation to the scenario.

public DatabaseScenarioBuilder<TEntryPoint, TDbContext> Apply(Action<TDbContext> action)

Parameters

action Action<TDbContext>

A non-null callback invoked once in arrangement order with the factory-owned scoped context. It must not retain or dispose the context. Tracked changes are saved after all actions finish.

Returns

DatabaseScenarioBuilder<TEntryPoint, TDbContext>

This builder, for chaining. The callback is retained until execution.

Apply(Func<TDbContext, CancellationToken, Task>)

Adds an asynchronous context mutation to the scenario.

public DatabaseScenarioBuilder<TEntryPoint, TDbContext> Apply(Func<TDbContext, CancellationToken, Task> action)

Parameters

action Func<TDbContext, CancellationToken, Task>

A non-null asynchronous callback invoked once in arrangement order with the factory-owned scoped context and execution token. It must not retain or dispose the context. Tracked changes are saved after all actions finish.

Returns

DatabaseScenarioBuilder<TEntryPoint, TDbContext>

This builder, for chaining. The callback is retained until execution.

EnsureCreated()

Ensures that the database schema exists before running subsequent actions.

public DatabaseScenarioBuilder<TEntryPoint, TDbContext> EnsureCreated()

Returns

DatabaseScenarioBuilder<TEntryPoint, TDbContext>

This builder, for chaining.

ExecuteAsync(CancellationToken)

Executes the arranged operations once and saves tracked changes.

public Task ExecuteAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

A token passed to database operations and asynchronous callbacks. The default token does not request cancellation.

Returns

Task

A task that completes after optional initialization or recreation, all actions, the final save, and any requested transaction commit. A second call throws an exception.

InTransaction()

Runs the arranged actions in a transaction supported by the configured provider.

public DatabaseScenarioBuilder<TEntryPoint, TDbContext> InTransaction()

Returns

DatabaseScenarioBuilder<TEntryPoint, TDbContext>

This builder, for chaining.

Remarks

The configured provider must support transactions. The transaction commits after all actions and the final save succeed; disposal rolls it back when execution fails before commit.

Recreate()

Deletes and recreates the isolated test database before subsequent actions.

public DatabaseScenarioBuilder<TEntryPoint, TDbContext> Recreate()

Returns

DatabaseScenarioBuilder<TEntryPoint, TDbContext>

This builder, for chaining.

RecreateDatabaseWith(Func<TDbContext, CancellationToken, Task>)

Uses an application-specific database recreation operation before subsequent actions.

public DatabaseScenarioBuilder<TEntryPoint, TDbContext> RecreateDatabaseWith(Func<TDbContext, CancellationToken, Task> recreateDatabase)

Parameters

recreateDatabase Func<TDbContext, CancellationToken, Task>

A non-null asynchronous callback invoked once before arranged actions. It receives the factory-owned scoped context and execution token and must not retain or dispose the context. The callback is responsible for deleting, migrating, restoring, or otherwise recreating the database; changes are saved after all arranged actions, not immediately after this callback.

Returns

DatabaseScenarioBuilder<TEntryPoint, TDbContext>

This builder, for chaining. The callback is retained until execution.

Seed<TEntity>(params TEntity[])

Adds entities during scenario execution.

public DatabaseScenarioBuilder<TEntryPoint, TDbContext> Seed<TEntity>(params TEntity[] entities) where TEntity : class

Parameters

entities TEntity[]

The non-null array of entities to add. Its contents are copied when this method is called; each element must be a valid entity accepted by EF Core.

Returns

DatabaseScenarioBuilder<TEntryPoint, TDbContext>

This builder, for chaining.

Type Parameters

TEntity

The mapped reference-entity type to add.