Show / Hide Table of Contents

Class DomainContext

A domain context that can be used to retrieve aggregates and save changes with entity framework.

Inheritance
Object
Microsoft.EntityFrameworkCore.DbContext
DomainContext
MultitenantDomainContext
Inherited Members
Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Internal.IDbSetCache.GetOrAddSet(Microsoft.EntityFrameworkCore.Internal.IDbSetSource, System.Type)
Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Internal.IDbQueryCache.GetOrAddQuery(Microsoft.EntityFrameworkCore.Internal.IDbQuerySource, System.Type)
Microsoft.EntityFrameworkCore.DbContext.Set<TEntity>()
Microsoft.EntityFrameworkCore.DbContext.Query<TQuery>()
Microsoft.EntityFrameworkCore.DbContext.OnConfiguring(Microsoft.EntityFrameworkCore.DbContextOptionsBuilder)
Microsoft.EntityFrameworkCore.DbContext.OnModelCreating(Microsoft.EntityFrameworkCore.ModelBuilder)
Microsoft.EntityFrameworkCore.DbContext.SaveChanges()
Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(System.Threading.CancellationToken)
Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Internal.IDbContextPoolable.SetPool(Microsoft.EntityFrameworkCore.Internal.IDbContextPool)
DbContext.IDbContextPoolable.SnapshotConfiguration()
Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Internal.IDbContextPoolable.Resurrect(Microsoft.EntityFrameworkCore.Internal.DbContextPoolConfigurationSnapshot)
Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Internal.IDbContextPoolable.ResetState()
Microsoft.EntityFrameworkCore.DbContext.Dispose()
Microsoft.EntityFrameworkCore.DbContext.Entry<TEntity>(TEntity)
DbContext.Entry(Object)
Microsoft.EntityFrameworkCore.DbContext.Add<TEntity>(TEntity)
Microsoft.EntityFrameworkCore.DbContext.AddAsync<TEntity>(TEntity, System.Threading.CancellationToken)
Microsoft.EntityFrameworkCore.DbContext.Attach<TEntity>(TEntity)
Microsoft.EntityFrameworkCore.DbContext.Update<TEntity>(TEntity)
Microsoft.EntityFrameworkCore.DbContext.Remove<TEntity>(TEntity)
DbContext.Add(Object)
Microsoft.EntityFrameworkCore.DbContext.AddAsync(System.Object, System.Threading.CancellationToken)
DbContext.Attach(Object)
Microsoft.EntityFrameworkCore.DbContext.Update(System.Object)
Microsoft.EntityFrameworkCore.DbContext.Remove(System.Object)
DbContext.AddRange(Object[])
Microsoft.EntityFrameworkCore.DbContext.AddRangeAsync(System.Object[])
Microsoft.EntityFrameworkCore.DbContext.AttachRange(System.Object[])
DbContext.UpdateRange(Object[])
DbContext.RemoveRange(Object[])
Microsoft.EntityFrameworkCore.DbContext.AddRange(System.Collections.Generic.IEnumerable<System.Object>)
DbContext.AddRangeAsync(IEnumerable<Object>, CancellationToken)
Microsoft.EntityFrameworkCore.DbContext.AttachRange(System.Collections.Generic.IEnumerable<System.Object>)
Microsoft.EntityFrameworkCore.DbContext.UpdateRange(System.Collections.Generic.IEnumerable<System.Object>)
Microsoft.EntityFrameworkCore.DbContext.RemoveRange(System.Collections.Generic.IEnumerable<System.Object>)
DbContext.Find(Type, Object[])
DbContext.FindAsync(Type, Object[])
Microsoft.EntityFrameworkCore.DbContext.FindAsync(System.Type, System.Object[], System.Threading.CancellationToken)
Microsoft.EntityFrameworkCore.DbContext.Find<TEntity>(System.Object[])
Microsoft.EntityFrameworkCore.DbContext.FindAsync<TEntity>(System.Object[])
Microsoft.EntityFrameworkCore.DbContext.FindAsync<TEntity>(System.Object[], System.Threading.CancellationToken)
Microsoft.EntityFrameworkCore.DbContext.Database
Microsoft.EntityFrameworkCore.DbContext.ChangeTracker
DbContext.Model
Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Internal.IDbContextDependencies.SetSource
Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Internal.IDbContextDependencies.QuerySource
DbContext.IDbContextDependencies.EntityFinderFactory
Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Internal.IDbContextDependencies.QueryProvider
Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Internal.IDbContextDependencies.StateManager
Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Internal.IDbContextDependencies.ChangeDetector
Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Internal.IDbContextDependencies.EntityGraphAttacher
Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Internal.IDbContextDependencies.UpdateLogger
Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Internal.IDbContextDependencies.InfrastructureLogger
Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.Instance
Object.Equals(Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()
Namespace: IRM.EntityFrameworkCore
Assembly: IRM.EntityFrameworkCore.dll
Syntax
public class DomainContext : DbContext, IInfrastructure<IServiceProvider>, IDbContextDependencies, IDbSetCache, IDbQueryCache, IDbContextPoolable, IDomainContext, IDbContext, IDisposable

Constructors

DomainContext(ICallContext)

Creates a new instance of DomainContext.

Declaration
protected DomainContext(ICallContext callContext)
Parameters
ICallContext callContext

The service used to access the current call context with information about tenant and user.

DomainContext(ICallContext, DbContextOptions)

Creates a new instance of DomainContext using the specified options.

Declaration
public DomainContext(ICallContext callContext, DbContextOptions options)
Parameters
ICallContext callContext

The service used to access the current call context with information about tenant and user.

Microsoft.EntityFrameworkCore.DbContextOptions options

The options for this context.

Properties

CallContext

Gets the ICallContext used to access information about tenant and user.

Declaration
protected ICallContext CallContext { get; }
Property Value
ICallContext

EventSource

Gets or sets an IEventSourceRepository used to store events.

Declaration
public virtual IEventSourceRepository EventSource { get; set; }
Property Value
IEventSourceRepository

Remarks

This must be set if events should be stored through the DomainContext.

When set in a cloud environment the connection string for the IEventSourceRepository is required to be exactly the same as for this DomainContext.

IsolationLevel

Gets or sets the IsolationLevel used for the transaction, when saving both data and events.

Declaration
public IsolationLevel IsolationLevel { get; set; }
Property Value
System.Transactions.IsolationLevel

MetadataProviders

The IMetadataProvider used when saving events for an aggregate in the EventSource.

Declaration
public virtual List<IMetadataProvider> MetadataProviders { get; }
Property Value
List<IMetadataProvider>

Remarks

By default a single AggregateMetadataProvider is configured.

TransactionTime

Gets the current transaction time. Default to System.DateTime.Now, but override it to set time in UTC.

Declaration
protected virtual DateTime TransactionTime { get; }
Property Value
System.DateTime

Methods

Add<T>(T, Boolean)

Adds an aggregate to the context so it can be saved in the database by calling SaveChanges(Boolean).

Declaration
public void Add<T>(T aggregateRoot, bool autoSave = true)
    where T : class, IAggregateRoot
Parameters
T aggregateRoot

The aggregate that should be added to the database.

Boolean autoSave

true if SaveChanges should be called automatically by Add; otherwise false.

Type Parameters
T

The type of aggregate.

Implements
IDomainContext.Add<T>(T, Boolean)

Delete<T>(T, Boolean)

Deletes (removes) an aggregate in the context so it can be deleted in the database by calling SaveChanges(Boolean).

Declaration
public void Delete<T>(T aggregateRoot, bool autoSave = true)
    where T : class, IAggregateRoot
Parameters
T aggregateRoot

The aggregate that should be deleted in the database.

Boolean autoSave

true if SaveChanges should be called automatically by Delete; otherwise false.

Type Parameters
T

The type of aggregate.

Implements
IDomainContext.Delete<T>(T, Boolean)

GetById<T>(Guid)

Gets an aggregate by unique identity from the database.

Declaration
public T GetById<T>(Guid id)
    where T : AggregateRoot
Parameters
System.Guid id

The unique identifier of the aggregate.

Returns
T

Returns an aggregate.

Type Parameters
T

The type of aggregate.

Implements
IDomainContext.GetById<T>(Guid)

ProcessEntityBeforeSave(EntityEntry<IEntity>, DateTime)

Extensibility point for additional processesing of an entity before the changes is persisted in the database.

Declaration
protected virtual void ProcessEntityBeforeSave(EntityEntry<IEntity> entityEntry, DateTime timeOfTransaction)
Parameters
Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<IEntity> entityEntry

The Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<TEntity> with change tracking information to process.

System.DateTime timeOfTransaction

The System.DateTime used for all entities in the current transaction.

Remarks

This method is called after the infrastrucutre code has ensured correct values of SenastÄndrad, SenastÄndradAv, Version and OwnerId. The method is called before events is saved.

ProcessEntityBeforeSaveAsync(EntityEntry<IEntity>, DateTime)

Extensibility point for additional processesing of an entity before the changes is persisted in the database.

Declaration
protected virtual Task ProcessEntityBeforeSaveAsync(EntityEntry<IEntity> entityEntry, DateTime timeOfTransaction)
Parameters
Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<IEntity> entityEntry

The Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<TEntity> with change tracking information to process.

System.DateTime timeOfTransaction

The System.DateTime used for all entities in the current transaction.

Returns
System.Threading.Tasks.Task

Remarks

This method is called after the infrastrucutre code has ensured correct values of SenastÄndrad, SenastÄndradAv, Version and OwnerId. The method is called before events is saved.

SaveChanges(Boolean)

Declaration
public override int SaveChanges(bool acceptAllChangesOnSuccess)
Parameters
Boolean acceptAllChangesOnSuccess

Returns
System.Int32

Overrides
DbContext.SaveChanges(Boolean)

SaveChangesAsync(Boolean, CancellationToken)

Declaration
public override async Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default(CancellationToken))
Parameters
Boolean acceptAllChangesOnSuccess

System.Threading.CancellationToken cancellationToken

Returns
System.Threading.Tasks.Task<System.Int32>

Overrides
DbContext.SaveChangesAsync(Boolean, CancellationToken)

SaveChangesCore(Boolean)

Saves all changes made in this context to the database.

Declaration
public virtual int SaveChangesCore(bool acceptAllChangesOnSuccess)
Parameters
Boolean acceptAllChangesOnSuccess

Indicates whether AcceptAllChanges() is called after the changes have been sent successfully to the database.

Returns
System.Int32

Remarks

SaveChanges(Boolean) uses the configured Microsoft.EntityFrameworkCore.Storage.IExecutionStrategy to call this method. If you need to control the Microsoft.EntityFrameworkCore.Storage.IExecutionStrategy yourself you should call this method instead of SaveChanges(Boolean).

SaveChangesCoreAsync(Boolean, CancellationToken)

Asynchronously saves all changes made in this context to the database.

Declaration
public virtual async Task<int> SaveChangesCoreAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default(CancellationToken))
Parameters
Boolean acceptAllChangesOnSuccess

Indicates whether AcceptAllChanges() is called after the changes have been sent successfully to the database.

System.Threading.CancellationToken cancellationToken

A System.Threading.CancellationToken to observe while waiting for the task to complete.

Returns
System.Threading.Tasks.Task<System.Int32>

Remarks

SaveChanges(Boolean) uses the configured Microsoft.EntityFrameworkCore.Storage.IExecutionStrategy to call this method. If you need to control the Microsoft.EntityFrameworkCore.Storage.IExecutionStrategy yourself you should call this method instead of SaveChanges(Boolean).

SetSomÄndrad(IEntity, DateTime)

Calls SetSomÄndrad(DateTime, String).

Declaration
protected virtual void SetSomÄndrad(IEntity entity, DateTime transaktionsTillfälle)
Parameters
IEntity entity

The IEntity.

System.DateTime transaktionsTillfälle

The System.DateTime representing this transaction.

Update<T>(T, Boolean)

Updates an aggregate in the context so it can be saved in the database by calling SaveChanges(Boolean)

Declaration
public void Update<T>(T aggregateRoot, bool autoSave = true)
    where T : class, IAggregateRoot
Parameters
T aggregateRoot

The aggregate that should be changed in the database.

Boolean autoSave

true if SaveChanges should be called automatically by Update; otherwise false.

Type Parameters
T

The type of aggregate.

Implements
IDomainContext.Update<T>(T, Boolean)

Extension Methods

DbContextExtensions.MigrateIfNeeded(DbContext)
DbContextExtensions.IsAllMigrationsApplied(DbContext)
DbContextExtensions.MigrateIfNeededAsync(DbContext)
DbContextExtensions.IsAllMigrationsAppliedAsync(DbContext)
DbContextExtensions.GetWithProcedure<TAggregateList>(DbContext, String, DbParameter[], ITranslate<TAggregateList, IDataReader>, Int32)
SqlExtensions.IsDefaultSchema(DbContext)
JsonExtensions.Serialize<T>(T)
JsonExtensions.Serialize<T>(T, JsonSerializerOptions)
Back to top Generated by DocFX