EntityFrameworkCore.GenericRepository.Nuget
1.0.0
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 EntityFrameworkCore.GenericRepository.Nuget --version 1.0.0
NuGet\Install-Package EntityFrameworkCore.GenericRepository.Nuget -Version 1.0.0
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="EntityFrameworkCore.GenericRepository.Nuget" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EntityFrameworkCore.GenericRepository.Nuget --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: EntityFrameworkCore.GenericRepository.Nuget, 1.0.0"
#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.
// Install EntityFrameworkCore.GenericRepository.Nuget as a Cake Addin
#addin nuget:?package=EntityFrameworkCore.GenericRepository.Nuget&version=1.0.0
// Install EntityFrameworkCore.GenericRepository.Nuget as a Cake Tool
#tool nuget:?package=EntityFrameworkCore.GenericRepository.Nuget&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Dependency
This library was created by .Net 7.0
Install
dotnet add package GenericRepository --version 1.0.0
Create Repository
public selead IUserRepository : IRepository<User>
public selead UserRepository : Repository<User, AppDbContext>, IUserRepository
Use
public selead UserService: IUserService
private readonly IUserRepository _userRepository
public UserService(IUserRepository userRepository)
{
_userRepository = userRepository;
}
public async Task AddAsync(User user, CancellationToken cancellationToken)
{
await _userRepository.AddAsync(user, cancellationToken);
}
Dependency Injection
builder.Service.AddScoped<IUnitOfWork, UnitOfWok>();
Methods
This library have two services. IRepository, IUnitOfWork
invoke
public interface IRepository<TEntity>
where TEntity : class
{
IQueryable<TEntity> GetAll();
IQueryable<TEntity> GetWhere(Expression<Func<TEntity, bool>> expression);
Task<TEntity> GetByExpressionAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default);
Task<TEntity> GetFirstAsync(CancellationToken cancellationToken = default);
TEntity GetByExpression(Expression<Func<TEntity, bool>> expression);
TEntity GetFirst();
Task AddAsync(TEntity entity, CancellationToken cancellationToken = default);
Task AddRangeASync(ICollection<TEntity> entities, CancellationToken cancellationToken = default);
void Update(TEntity entity);
void UpdateRange(ICollection<TEntity> entities);
Task DeleteByIdAsync(string id);
Task DeleteByExpressionAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default);
void Delete(TEntity entity);
void DeleteRange(ICollection<TEntity> entities);
}
public sealed class Repository<TEntity, TContext> : IRepository<TEntity>
where TEntity : class
where TContext : DbContext
{
private readonly TContext _context;
private DbSet<TEntity> Entity;
public Repository(TContext context)
{
_context = context;
Entity = _context.Set<TEntity>();
}
public async Task AddAsync(TEntity entity, CancellationToken cancellationToken = default)
{
await Entity.AddAsync(entity, cancellationToken).ConfigureAwait(false);
}
public async Task AddRangeASync(ICollection<TEntity> entities, CancellationToken cancellationToken = default)
{
await Entity.AddRangeAsync(entities, cancellationToken).ConfigureAwait(false);
}
public void Delete(TEntity entity)
{
Entity.Remove(entity);
}
public async Task DeleteByExpressionAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default)
{
TEntity entity = await Entity.Where(expression).AsNoTracking().FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false);
Entity.Remove(entity);
}
public async Task DeleteByIdAsync(string id)
{
TEntity entity = await Entity.FindAsync(id).ConfigureAwait(false);
Entity.Remove(entity);
}
public void DeleteRange(ICollection<TEntity> entities)
{
Entity.RemoveRange(entities);
}
public IQueryable<TEntity> GetAll()
{
return Entity.AsNoTracking().AsQueryable();
}
public TEntity GetByExpression(Expression<Func<TEntity, bool>> expression)
{
TEntity entity = Entity.Where(expression).AsNoTracking().FirstOrDefault();
return entity;
}
public async Task<TEntity> GetByExpressionAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default)
{
TEntity entity = await Entity.Where(expression).AsNoTracking().FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false);
return entity;
}
public TEntity GetFirst()
{
TEntity entity = Entity.AsNoTracking().FirstOrDefault();
return entity;
}
public async Task<TEntity> GetFirstAsync(CancellationToken cancellationToken = default)
{
TEntity entity = await Entity.AsNoTracking().FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false);
return entity;
}
public IQueryable<TEntity> GetWhere(Expression<Func<TEntity, bool>> expression)
{
return Entity.AsNoTracking().Where(expression).AsQueryable();
}
public void Update(TEntity entity)
{
Entity.Update(entity);
}
public void UpdateRange(ICollection<TEntity> entities)
{
Entity.UpdateRange(entities);
}
}
public interface IUnitOfWork
{
Task SaveChangesAsync(CancellationToken cancellationToken);
void SaveChanges();
}
public sealed class UnitOfWork<TContext> : IUnitOfWork
where TContext: DbContext
{
private readonly TContext _context;
public UnitOfWork(TContext context)
{
_context = context;
}
public void SaveChanges()
{
_context.SaveChanges();
}
public async Task SaveChangesAsync(CancellationToken cancellationToken)
{
await _context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net7.0
- Microsoft.EntityFrameworkCore (>= 7.0.5)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.