Class EfCoreDbContext<TMarker>
- Namespace
- Savvyio.Extensions.DependencyInjection.EFCore
- Assembly
- Savvyio.Extensions.DependencyInjection.EFCore.dll
Provides a default implementation of the DbContext class to support Savvy I/O extensions of Microsoft Entity Framework Core in multiple implementations.
public class EfCoreDbContext<TMarker> : EfCoreDbContext, IInfrastructure<IServiceProvider>, IDbContextDependencies, IDbSetCache, IDbContextPoolable, IResettableService, IDisposable, IAsyncDisposable, IConfigurable<EfCoreDataSourceOptions>, IDependencyInjectionMarker<TMarker>
Type Parameters
TMarkerThe type used to mark the implementation that this context represents. Optimized for Microsoft Dependency Injection.
- Inheritance
-
EfCoreDbContext<TMarker>
- Implements
-
IDependencyInjectionMarker<TMarker>
- Inherited Members
Examples
This example shows how EfCoreDbContext<TMarker> can be used when a DI marker needs its own EF Core context instance.
using System;
using Microsoft.EntityFrameworkCore;
using Savvyio;
using Savvyio.Extensions.DependencyInjection.EFCore;
namespace ExampleApp;
public sealed class MarkerContextFactory
{
public OrdersDbContext Create()
{
var options = new EfCoreDataSourceOptions<OrdersMarker>
{
ContextConfigurator = builder => builder.EnableDetailedErrors(),
ModelConstructor = modelBuilder => modelBuilder.Entity<OrderRecord>().HasKey(order => order.Id)
};
return new OrdersDbContext(options);
}
}
public sealed class OrdersDbContext : EfCoreDbContext<OrdersMarker>
{
public OrdersDbContext(EfCoreDataSourceOptions<OrdersMarker> options) : base(options)
{
}
public DbSet<OrderRecord> Orders => Set<OrderRecord>();
}
public sealed class OrdersMarker
{
}
public sealed class OrderRecord : IIdentity<Guid>
{
public OrderRecord(Guid id, string orderNumber)
{
Id = id;
OrderNumber = orderNumber;
}
public Guid Id { get; }
public string OrderNumber { get; private set; } = string.Empty;
}
Constructors
EfCoreDbContext(EfCoreDataSourceOptions<TMarker>)
Initializes a new instance of the EfCoreDbContext<TMarker> class.
public EfCoreDbContext(EfCoreDataSourceOptions<TMarker> options)
Parameters
optionsEfCoreDataSourceOptions<TMarker>The EfCoreDataSourceOptions<TMarker> used to configure this instance.