EfCoreDbContext Class
Definition
- Namespace
- Savvyio.Extensions.EFCore
- Assemblies
- Savvyio.Extensions.EFCore.dll
Provides a default implementation of the DbContext class to support Savvy I/O extensions of Microsoft Entity Framework Core.
public class EfCoreDbContext : DbContext, IInfrastructure<IServiceProvider>, IDbContextDependencies, IDbSetCache, IDbContextPoolable, IResettableService, IDisposable, IAsyncDisposable, IConfigurable<EfCoreDataSourceOptions>
- Inheritance
-
objectEfCoreDbContext
- Implements
- Derived
- Inherited Members
Examples
EfCoreDbContext is the Savvy I/O base context that accepts EfCoreDataSourceOptions for connection management and configuration. Subclass it to add DbSet<T> properties for your domain entities and override OnModelCreating for fluent EF Core configuration. The example defines a minimal order context using an in-memory provider and verifies the context can be created and disposed.
using System;
using Microsoft.EntityFrameworkCore;
using Savvyio;
using Savvyio.Extensions.EFCore;
namespace ExampleApp;
public sealed class CatalogContextFactory
{
public CatalogDbContext Create()
{
var options = new EfCoreDataSourceOptions
{
ContextConfigurator = builder => builder.EnableDetailedErrors(),
ModelConstructor = modelBuilder => modelBuilder.Entity<Product>().HasKey(product => product.Id)
};
return new CatalogDbContext(options);
}
}
public sealed class CatalogDbContext : EfCoreDbContext
{
public CatalogDbContext(EfCoreDataSourceOptions options) : base(options)
{
}
public DbSet<Product> Products => Set<Product>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Product>().HasKey(product => product.Id);
}
}
public sealed class Product : IIdentity<Guid>
{
public Product(Guid id, string name)
{
Id = id;
Name = name;
}
public Guid Id { get; }
public string Name { get; private set; } = string.Empty;
}
Constructors
| Name | Description |
|---|---|
| EfCoreDbContext(EfCoreDataSourceOptions) | Initializes a new instance of the EfCoreDbContext class. |
Properties
| Name | Description |
|---|---|
| Options | Gets the configured options of this instance. |
Methods
| Name | Description |
|---|---|
| ConfigureConventions(ModelConfigurationBuilder) | Override this method to set defaults and configure conventions before they run. This method is invoked before OnModelCreating(ModelBuilder). |
| OnConfiguring(DbContextOptionsBuilder) | Override this method to configure the database (and other options) to be used for this context. This method is called for each instance of the context that is created. The base implementation does nothing. In situations where an instance of DbContextOptions may or may not have been passed to the constructor, you can use IsConfigured to determine if the options have already been set, and skip some or all of the logic in OnConfiguring(DbContextOptionsBuilder). |
| OnModelCreating(ModelBuilder) | Override this method to further configure the model that was discovered by convention from the entity types exposed in DbSet<TEntity> properties on your derived context. The resulting model may be cached and re-used for subsequent instances of your derived context. |