Search Results for

    Class DbContextBuilder<T>

    Uses the Builder pattern to create instances of DbContext types seeded with specified data.

    Inheritance
    object
    DbContextBuilder<T>
    Implements
    IDisposable
    Inherited Members
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    object.ToString()
    Namespace: Wolfgang.DbContextBuilderCore
    Assembly: Wolfgang.DbContextBuilder-Core.dll
    Syntax
    public class DbContextBuilder<T> : IDisposable where T : DbContext
    Type Parameters
    Name Description
    T

    The concrete DbContext type to construct.

    Remarks

    When using the SQLite provider, the builder holds an open SQLite in-memory connection. Dispose the builder only after all DbContext instances returned by BuildAsync() are no longer in use, as disposing the builder closes the shared connection and destroys the in-memory database.

    Methods

    | Edit this page View Source

    BuildAsync()

    Creates a new instance of T seeded with the specified data.

    Declaration
    public Task<T> BuildAsync()
    Returns
    Type Description
    Task<T>

    A new instance of T.

    Exceptions
    Type Condition
    ObjectDisposedException

    The builder has been disposed.

    | Edit this page View Source

    Dispose()

    Disposes the underlying database context creator, releasing any held resources (e.g., the SQLite in-memory connection).

    Declaration
    public void Dispose()
    | Edit this page View Source

    Dispose(bool)

    Releases unmanaged and optionally managed resources.

    Declaration
    protected virtual void Dispose(bool disposing)
    Parameters
    Type Name Description
    bool disposing

    true to release both managed and unmanaged resources.

    | Edit this page View Source

    SeedWithRandom<TEntity>(int)

    Populates the specified DbSet with random entities of type TEntity.

    Declaration
    public DbContextBuilder<T> SeedWithRandom<TEntity>(int count) where TEntity : class
    Parameters
    Type Name Description
    int count

    The number of items to create

    Returns
    Type Description
    DbContextBuilder<T>

    DbContextBuilder<T>

    Type Parameters
    Name Description
    TEntity

    The type of entity to create

    Remarks

    Foreign keys on the generated entities are reconciled against the model when the context is built: a required FK is wired to a seeded principal of its type (so seed the principals too), and an optional FK with no seeded principal is set to null. The FK values on a randomly-seeded entity are therefore not the raw random values produced by the creator. Entities added via SeedWith are never reconciled.

    Exceptions
    Type Condition
    ArgumentOutOfRangeException

    count is less than 1

    | Edit this page View Source

    SeedWithRandom<TEntity>(int, Func<TEntity, int, TEntity>)

    Populates the specified DbSet with random entities of type TEntity.

    Declaration
    public DbContextBuilder<T> SeedWithRandom<TEntity>(int count, Func<TEntity, int, TEntity> func) where TEntity : class
    Parameters
    Type Name Description
    int count

    The number of items to create

    Func<TEntity, int, TEntity> func

    A function that takes a TEntity and the index number of the entity and returns an updated TEntity

    Returns
    Type Description
    DbContextBuilder<T>

    DbContextBuilder<T>

    Type Parameters
    Name Description
    TEntity

    The type of entity to create

    Remarks

    Foreign keys on the generated entities are reconciled against the model when the context is built: a required FK is wired to a seeded principal of its type (so seed the principals too), and an optional FK with no seeded principal is set to null. The FK values on a randomly-seeded entity are therefore not the raw random values produced by the creator. Entities added via SeedWith are never reconciled.

    Exceptions
    Type Condition
    ArgumentOutOfRangeException

    count is less than 1

    | Edit this page View Source

    SeedWithRandom<TEntity>(int, Func<TEntity, TEntity>)

    Populates the specified DbSet with random entities of type TEntity.

    Declaration
    public DbContextBuilder<T> SeedWithRandom<TEntity>(int count, Func<TEntity, TEntity> func) where TEntity : class
    Parameters
    Type Name Description
    int count

    The number of items to create

    Func<TEntity, TEntity> func

    A function that takes a TEntity and returns an updated TEntity

    Returns
    Type Description
    DbContextBuilder<T>

    DbContextBuilder<T>

    Type Parameters
    Name Description
    TEntity

    The type of entity to create

    Remarks

    Foreign keys on the generated entities are reconciled against the model when the context is built: a required FK is wired to a seeded principal of its type (so seed the principals too), and an optional FK with no seeded principal is set to null. The FK values on a randomly-seeded entity are therefore not the raw random values produced by the creator. Entities added via SeedWith are never reconciled.

    Exceptions
    Type Condition
    ArgumentOutOfRangeException

    count is less than 1

    | Edit this page View Source

    SeedWith<TEntity>(IEnumerable<TEntity>)

    Populates the specified DbSet with the provided entities.

    Declaration
    public DbContextBuilder<T> SeedWith<TEntity>(IEnumerable<TEntity> entities) where TEntity : class
    Parameters
    Type Name Description
    IEnumerable<TEntity> entities

    The entities to populate the database with

    Returns
    Type Description
    DbContextBuilder<T>

    DbContextBuilder<T>

    Type Parameters
    Name Description
    TEntity
    Remarks

    Insertion order across distinct entity types is not guaranteed — the builder accumulates seeds in a single list and EF's SaveChangesAsync orders the inserts by FK dependency, not by the order SeedWith calls were made. For scenarios where the order of two same-type rows matters (e.g. identity-generated keys), pass them in the desired order within a single SeedWith call. Inheritance mapping (TPH / TPT / TPC) is supported via entity.GetType(); the runtime type determines which DbSet receives the row.

    Exceptions
    Type Condition
    ArgumentNullException

    entities is null

    ArgumentException

    entities contains a null item

    ArgumentException

    entities contains a string

    | Edit this page View Source

    SeedWith<TEntity>(TEntity)

    Populates the specified DbSet with a single entity. Equivalent to calling the params-array overload with one element, but avoids the per-call allocation of a one-element array — useful in tests that seed many single rows.

    Declaration
    public DbContextBuilder<T> SeedWith<TEntity>(TEntity entity) where TEntity : class
    Parameters
    Type Name Description
    TEntity entity

    The entity to populate the database with.

    Returns
    Type Description
    DbContextBuilder<T>

    The builder, for chaining.

    Type Parameters
    Name Description
    TEntity
    Exceptions
    Type Condition
    ArgumentNullException

    entity is null.

    ArgumentException

    entity is a string instance (matches the params overload's rejection regardless of how TEntity was inferred).

    | Edit this page View Source

    SeedWith<TEntity>(params TEntity[])

    Populates the specified DbSet with the provided entities.

    Declaration
    public DbContextBuilder<T> SeedWith<TEntity>(params TEntity[] entities) where TEntity : class
    Parameters
    Type Name Description
    TEntity[] entities

    The entities to populate the database with

    Returns
    Type Description
    DbContextBuilder<T>

    DbContextBuilder<T>

    Type Parameters
    Name Description
    TEntity
    Exceptions
    Type Condition
    ArgumentNullException

    entities is null

    ArgumentException

    entities contains a null item

    ArgumentException

    entities contains a string

    | Edit this page View Source

    UseCustomRandomEntityCreator(ICreateRandomEntities)

    Allows the user to specify their own implementation of ICreateRandomEntities for creating random entities.

    Declaration
    public DbContextBuilder<T> UseCustomRandomEntityCreator(ICreateRandomEntities creator)
    Parameters
    Type Name Description
    ICreateRandomEntities creator

    The creator to use

    Returns
    Type Description
    DbContextBuilder<T>

    DbContextBuilder<T>

    | Edit this page View Source

    UseDbContextOptionsBuilder(DbContextOptionsBuilder<T>)

    Specifies a specific DbContextOptionsBuilder<TContext> instance to use when creating the DbContext.

    Declaration
    public DbContextBuilder<T> UseDbContextOptionsBuilder(DbContextOptionsBuilder<T> dbContextOptionsBuilder)
    Parameters
    Type Name Description
    DbContextOptionsBuilder<T> dbContextOptionsBuilder

    The options builder to use when creating the DbContext.

    Returns
    Type Description
    DbContextBuilder<T>

    DbContextBuilder<T>

    Exceptions
    Type Condition
    ArgumentNullException

    dbContextOptionsBuilder is null.

    | Edit this page View Source

    UseDiagnosticOutput(Action<string>)

    Routes diagnostic output to writeLine: EF Core logs (including the generated SQL) produced while creating and seeding the database, plus a one-line summary of how many entity rows were seeded. Pass your test framework's output sink — for example UseDiagnosticOutput(testOutputHelper.WriteLine) in xUnit — so the seeded context is visible in the test log when an assertion fails.

    Declaration
    public DbContextBuilder<T> UseDiagnosticOutput(Action<string> writeLine)
    Parameters
    Type Name Description
    Action<string> writeLine

    Receives each diagnostic line.

    Returns
    Type Description
    DbContextBuilder<T>

    DbContextBuilder<T>

    Exceptions
    Type Condition
    ArgumentNullException

    writeLine is null.

    | Edit this page View Source

    UseInMemory()

    Instructs the builder to use InMemory as the database provider.

    Declaration
    public DbContextBuilder<T> UseInMemory()
    Returns
    Type Description
    DbContextBuilder<T>

    DbContextBuilder<T>

    Remarks

    Provider selection is last-write-wins — calling UseInMemory() after a previous call to either UseInMemory() or a SQLite extension overrides the earlier choice. Choose one provider per builder.

    | Edit this page View Source

    UseSeedProfile(ISeedProfile<T>)

    Applies a reusable ISeedProfile<T> to this builder. Profiles bundle a complete set of seed data so the same setup can be shared across many tests with a single call. Multiple profiles can be applied; their seed data accumulates.

    Declaration
    public DbContextBuilder<T> UseSeedProfile(ISeedProfile<T> profile)
    Parameters
    Type Name Description
    ISeedProfile<T> profile

    The seed profile to apply.

    Returns
    Type Description
    DbContextBuilder<T>

    DbContextBuilder<T>

    Exceptions
    Type Condition
    ArgumentNullException

    profile is null.

    Implements

    IDisposable

    Extension Methods

    DbContextBuilderSqliteExtensions.UseSqliteForMsSqlServer<TDbContext>(DbContextBuilder<TDbContext>)
    DbContextBuilderSqliteExtensions.UseSqlite<TDbContext>(DbContextBuilder<TDbContext>)
    • Edit this page
    • View Source
    In this article
    Back to top Generated by DocFX