Class EfCoreDbContext
- Namespace
- Savvyio.Extensions.EFCore
- Assembly
- 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
-
EfCoreDbContext
- 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
EfCoreDbContext(EfCoreDataSourceOptions)
Initializes a new instance of the EfCoreDbContext class.
public EfCoreDbContext(EfCoreDataSourceOptions options)
Parameters
optionsEfCoreDataSourceOptionsThe EfCoreDataSourceOptions used to configure this instance.
Properties
Options
Gets the configured options of this instance.
public EfCoreDataSourceOptions Options { get; }
Property Value
- EfCoreDataSourceOptions
The configured options of this instance.
Methods
ConfigureConventions(ModelConfigurationBuilder)
Override this method to set defaults and configure conventions before they run. This method is invoked before OnModelCreating(ModelBuilder).
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
Parameters
configurationBuilderModelConfigurationBuilderThe builder being used to set defaults and configure conventions that will be used to build the model for this context.
Remarks
If a model is explicitly set on the options for this context (via UseModel(IModel)) then this method will not be run.
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).
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
Parameters
optionsBuilderDbContextOptionsBuilderA builder used to create or modify options for this context. Databases (and other extensions) typically define extension methods on this object that allow you to configure the context.
Remarks
See DbContext lifetime, configuration, and initialization for more information.
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.
protected override void OnModelCreating(ModelBuilder modelBuilder)
Parameters
modelBuilderModelBuilderThe builder being used to construct the model for this context. Databases (and other extensions) typically define extension methods on this object that allow you to configure aspects of the model that are specific to a given database.
Remarks
If a model is explicitly set on the options for this context (via UseModel(IModel)) then this method will not be run.
See Modeling entity types and relationships for more information.