Table of Contents

Class KafkaDbContext

Namespace
MASES.EntityFrameworkCore.KNet.Infrastructure
Assembly
MASES.EntityFrameworkCore.KNet.dll

A KafkaDbContext instance represents a session with the Apache Kafka cluster and can be used to query and save instances of your entities. KafkaDbContext extends DbContext and it is a combination of the Unit Of Work and Repository patterns.

public class KafkaDbContext : DbContext, IInfrastructure<IServiceProvider>, IDbContextDependencies, IDbSetCache, IDbContextPoolable, IResettableService, IDisposable, IAsyncDisposable
Inheritance
KafkaDbContext
Implements
Derived
Inherited Members

Remarks

Entity Framework Core does not support multiple parallel operations being run on the same KafkaDbContext instance. This includes both parallel execution of async queries and any explicit concurrent use from multiple threads. Therefore, always await async calls immediately, or use separate DbContext instances for operations that execute in parallel. See Avoiding DbContext threading issues for more information and examples.

Typically you create a class that derives from DbContext and contains DbSet<TEntity> properties for each entity in the model. If the DbSet<TEntity> properties have a public setter, they are automatically initialized when the instance of the derived context is created.

Typically you don't need to override the OnConfiguring(DbContextOptionsBuilder) method to configure the database (and other options) to be used for the context, just set the properties associated to KafkaDbContext class. Alternatively, if you would rather perform configuration externally instead of inline in your context, you can use DbContextOptionsBuilder<TContext> (or DbContextOptionsBuilder) to externally create an instance of DbContextOptions<TContext> (or DbContextOptions) and pass it to a base constructor of DbContext.

The model is discovered by running a set of conventions over the entity classes found in the DbSet<TEntity> properties on the derived context. To further configure the model that is discovered by convention, you can override the OnModelCreating(ModelBuilder) method.

See KafkaDbContext configuration and initialization, DbContext lifetime, configuration, and initialization, Querying data with EF Core, Changing tracking, and Saving data with EF Core for more information and examples.

Constructors

KafkaDbContext()

Initializes a new instance of the DbContext class. The OnConfiguring(DbContextOptionsBuilder) method will be called to configure the database (and other options) to be used for this context.

public KafkaDbContext()

Remarks

See DbContext lifetime, configuration, and initialization for more information and examples.

KafkaDbContext(DbContextOptions)

Initializes a new instance of the DbContext class using the specified options. The OnConfiguring(DbContextOptionsBuilder) method will still be called to allow further configuration of the options.

public KafkaDbContext(DbContextOptions options)

Parameters

options DbContextOptions

The options for this context.

Remarks

Fields

IsPerformanceVersion

Reports if the library was compiled to reports performance information

public const bool IsPerformanceVersion = false

Field Value

bool

Properties

ApplicationId

The application id

public virtual string ApplicationId { get; set; }

Property Value

string

BootstrapServers

The bootstrap servers of the Apache Kafka cluster

public virtual string? BootstrapServers { get; set; }

Property Value

string

ConsumerConfig

public virtual ConsumerConfigBuilder? ConsumerConfig { get; set; }

Property Value

ConsumerConfigBuilder

DatabaseName

Database name means whe prefix of the topics associated to the instance of KafkaDbContext

public virtual string? DatabaseName { get; set; }

Property Value

string

DefaultConsumerConfig

The default ConsumerConfig configuration

public static ConsumerConfigBuilder DefaultConsumerConfig { get; }

Property Value

ConsumerConfigBuilder

The default ConsumerConfig configuration.

DefaultConsumerInstances

Default consumr instances used in conjunction with UseCompactedReplicator

public virtual int? DefaultConsumerInstances { get; set; }

Property Value

int?

DefaultNumPartitions

Default number of partitions associated to each topic

public virtual int DefaultNumPartitions { get; set; }

Property Value

int

DefaultProducerConfig

The default ProducerConfig configuration

public static ProducerConfigBuilder DefaultProducerConfig { get; }

Property Value

ProducerConfigBuilder

The default ProducerConfig configuration.

DefaultReplicationFactor

Default replication factor associated to each topic

public virtual short DefaultReplicationFactor { get; set; }

Property Value

short

DefaultStreamsConfig

The default StreamsConfig configuration

public static StreamsConfigBuilder DefaultStreamsConfig { get; }

Property Value

StreamsConfigBuilder

The default StreamsConfig configuration.

DefaultTopicConfig

The default TopicConfig configuration

public static TopicConfigBuilder DefaultTopicConfig { get; }

Property Value

TopicConfigBuilder

The default TopicConfig configuration.

EnableKEFCoreTracing

Set to true to enable tracing of KEFCore

public static bool EnableKEFCoreTracing { get; set; }

Property Value

bool

Remarks

Can be set only if the project is compiled with DEBUG_PERFORMANCE preprocessor directive, otherwise an InvalidOperationException is raised

KeySerDesSelectorType

The optional Type to use for key serialization selection

Default value is DefaultKEFCoreSerDes.Key<T>, any custom Type shall implement ISerDesSelector<T>

public virtual Type? KeySerDesSelectorType { get; set; }

Property Value

Type

OnChangeEvent

The optional handler to be used to receive notification when the back-end triggers a data change.

public virtual Action<EntityTypeChanged>? OnChangeEvent { get; set; }

Property Value

Action<EntityTypeChanged>

Remarks

ProducerConfig

The optional ProducerConfigBuilder

public virtual ProducerConfigBuilder? ProducerConfig { get; set; }

Property Value

ProducerConfigBuilder

StreamsConfig

The optional StreamsConfig used when UseCompactedReplicator is false

public virtual StreamsConfigBuilder? StreamsConfig { get; set; }

Property Value

StreamsConfigBuilder

TopicConfig

The optional TopicConfigBuilder used when topics shall be created

public virtual TopicConfigBuilder? TopicConfig { get; set; }

Property Value

TopicConfigBuilder

UseByteBufferDataTransfer

Setting this property to true the engine prefers to use ByteBuffer data exchange in serializer instances

public virtual bool UseByteBufferDataTransfer { get; set; }

Property Value

bool

UseCompactedReplicator

Use KNetCompactedReplicator<K, V> instead of Apache Kafka Streams

public virtual bool UseCompactedReplicator { get; set; }

Property Value

bool

UseDeletePolicyForTopic

Use delete cleanup policy when a topic is created

public bool UseDeletePolicyForTopic { get; set; }

Property Value

bool

UseEnumeratorWithPrefetch

Setting this property to true the engine prefers to use enumerator instances able to do a prefetch on data speeding up execution

public virtual bool UseEnumeratorWithPrefetch { get; set; }

Property Value

bool

Remarks

Used only if UseCompactedReplicator is false and UseKNetStreams is true, not available in EFCore 6.

UseKNetStreams

Use KNet version of Apache Kafka Streams instead of standard Apache Kafka Streams

public virtual bool UseKNetStreams { get; set; }

Property Value

bool

UseNameMatching

Set to false to avoid match of IEntityTypes using Name

public virtual bool UseNameMatching { get; set; }

Property Value

bool

UsePersistentStorage

Use persistent storage when Apache Kafka Streams is in use

public virtual bool UsePersistentStorage { get; set; }

Property Value

bool

ValueContainerType

The optional Type to use as value container

Default value is DefaultValueContainer<TKey>, any custom Type shall implement IValueContainer<T>

public virtual Type? ValueContainerType { get; set; }

Property Value

Type

ValueSerDesSelectorType

The optional Type to use for value serialization selection

Default value is DefaultKEFCoreSerDes.ValueContainer<T>, any custom Type shall implement ISerDesSelector<T>

public virtual Type? ValueSerDesSelectorType { get; set; }

Property Value

Type

Methods

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.

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

Parameters

optionsBuilder DbContextOptionsBuilder

A 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

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).

See DbContext lifetime, configuration, and initialization for more information and examples.