Elephant.Database
0.3.2
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Elephant.Database --version 0.3.2
NuGet\Install-Package Elephant.Database -Version 0.3.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Elephant.Database" Version="0.3.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Elephant.Database" Version="0.3.2" />
<PackageReference Include="Elephant.Database" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Elephant.Database --version 0.3.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Elephant.Database, 0.3.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Elephant.Database@0.3.2
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Elephant.Database&version=0.3.2
#tool nuget:?package=Elephant.Database&version=0.3.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
About
Contains various EF Core related database helpers.
DbSetExtensions
Clear(): Remove all entities from that DbSet.
GenericCrud
Task<List<TEntity>> All(CancellationToken cancellationToken);
// Example usage: await myRepository.All(1, QueryTrackingBehavior.NoTracking, CancellationToken.None, x => x.CustomersCrossOrders, x => x.CustomersCrossAddresses);
Task<List<TEntity>> All(QueryTrackingBehavior queryTrackingBehavior = QueryTrackingBehavior.TrackAll, CancellationToken cancellationToken = default, params Expression<Func<TEntity, object>>[] includes);
Task<TEntity?> ById(object id, CancellationToken cancellationToken);
Task<int> Count(CancellationToken cancellationToken);
Task<int> Count(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken);
void Delete(object id);
Task<ResponseWrapper<int>> DeleteAndSave(object id, CancellationToken cancellationToken);
Task<bool> HasAny(CancellationToken cancellationToken);
Task<bool> HasAny(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken);
Task Insert(TEntity obj, CancellationToken cancellationToken);
Task Insert(ICollection<TEntity> objects, CancellationToken cancellationToken);
Task<ResponseWrapper<int>> InsertAndSave(TEntity obj, CancellationToken cancellationToken);
Task<ResponseWrapper<int>> Save(CancellationToken cancellationToken);
void Update(TEntity obj);
void Update(ICollection<TEntity> objects);
Task<ResponseWrapper<int>> UpdateAndSave(TEntity obj, CancellationToken cancellationToken);
Task DeleteAllAndResetAutoIncrement(CancellationToken cancellationToken = default, string schema = "dbo");
// Transactions:
Task<IDbContextTransaction> BeginTransactionAsync(CancellationToken cancellationToken);
Task CommitTransactionAsyncAndDispose(IDbContextTransaction? transaction, CancellationToken cancellationToken);
Task RollbackTransactionAsyncAndDispose(IDbContextTransaction? transaction, CancellationToken cancellationToken);
Example IContext transaction implementation
// Usings
using Elephant.Database;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
// Inside your DbContext or IdentityDbContext class:
/// <inheritdoc/>
public async Task<IDbContextTransaction> BeginTransaction(CancellationToken cancellationToken)
{
return await Database.BeginTransactionAsync(cancellationToken);
}
/// <inheritdoc/>
public async Task CommitTransactionAndDispose(IDbContextTransaction? transaction, CancellationToken cancellationToken)
{
if (transaction == null)
return;
await Database.CommitTransactionAsync(cancellationToken);
await transaction.DisposeAsync();
}
/// <inheritdoc/>
public async Task RollbackTransactionAndDispose(IDbContextTransaction? transaction, CancellationToken cancellationToken)
{
if (transaction == null)
return;
await Database.RollbackTransactionAsync(cancellationToken);
await transaction.DisposeAsync();
}
Example transaction in your service
using (IDbContextTransaction transaction = await _customerRepository.BeginTransaction(cancellationToken))
{
try
{
// Your code here.
}
catch (Exception exception)
{
_logger.LogError($"An error occurred. Rolling back changes. Exception: {exception}".);
await _customerRepository.RollbackTransactionAndDispose(transaction, cancellationToken);
return;
}
// Perform CRUD actions or whatever.
await _customerRepository.Save(cancellationToken);
await _customerRepository.CommitTransactionAndDispose(transaction, cancellationToken);
}
QueryableExtensions
IQueryable<T> AsTracking<T>(this IQueryable<T> source, bool isTracked) { .. }
IQueryable<T> OrderByColumn<T>(this IQueryable<T> source, string columnName, ListSortDirection sortDirection = ListSortDirection.Ascending) { .. }
IQueryable<T> OrderByColumn<T>(this IQueryable<T> source, string columnName, bool isAscending = true) { .. }
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net6.0
- Elephant.Types (>= 1.1.1)
- Microsoft.AspNetCore.Http (>= 2.2.2)
- Microsoft.EntityFrameworkCore (>= 7.0.0)
-
net7.0
- Elephant.Types (>= 1.1.1)
- Microsoft.AspNetCore.Http (>= 2.2.2)
- Microsoft.EntityFrameworkCore (>= 7.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Update README.md and transaction rename.