Table of Contents

Struct DbContextId

Namespace
Microsoft.EntityFrameworkCore
Assembly
Microsoft.EntityFrameworkCore.dll

A unique identifier for the context instance and pool lease, if any.

public readonly struct DbContextId
Inherited Members

Remarks

This identifier is primarily intended as a correlation ID for logging and debugging such that it is easy to identify that multiple events are using the same or different context instances.

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

Constructors

DbContextId(Guid, int)

Creates a new DbContextId with the given InstanceId and lease number.

public DbContextId(Guid id, int lease)

Parameters

id Guid

A unique identifier for the DbContext being used.

lease int

A number indicating whether this is the first, second, third, etc. lease of this instance.

Properties

InstanceId

A unique identifier for the DbContext being used.

public Guid InstanceId { get; }

Property Value

Guid

Remarks

When context pooling is being used, then this ID must be combined with the Lease in order to get a unique ID for the effective instance being used.

Lease

A number that is incremented each time this particular DbContext instance is leased from the context pool.

public int Lease { get; }

Property Value

int

Remarks

Will be zero if context pooling is not being used.

Methods

Equals(DbContextId)

Compares this ID to another ID to see if they represent the same leased context.

public bool Equals(DbContextId other)

Parameters

other DbContextId

The other ID.

Returns

bool

true if they represent the same leased context; false otherwise.

Equals(object?)

Compares this ID to another ID to see if they represent the same leased context.

public override bool Equals(object? obj)

Parameters

obj object

The other ID.

Returns

bool

true if they represent the same leased context; false otherwise.

GetHashCode()

A hash code for this ID.

public override int GetHashCode()

Returns

int

The hash code.

ToString()

Returns the instance ID and lease number.

public override string ToString()

Returns

string

The instance ID and lease number.

Operators

operator ==(DbContextId, DbContextId)

Compares one ID to another ID to see if they represent the same leased context.

public static bool operator ==(DbContextId left, DbContextId right)

Parameters

left DbContextId

The first ID.

right DbContextId

The second ID.

Returns

bool

true if they represent the same leased context; false otherwise.

operator !=(DbContextId, DbContextId)

Compares one ID to another ID to see if they represent different leased contexts.

public static bool operator !=(DbContextId left, DbContextId right)

Parameters

left DbContextId

The first ID.

right DbContextId

The second ID.

Returns

bool

true if they represent different leased contexts; false otherwise.