86 lines
2.1 KiB
C#
86 lines
2.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Linq.Expressions;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using _0_Framework.Domain;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Storage;
|
|
|
|
namespace _0_Framework.InfraStructure
|
|
{
|
|
public class RepositoryBase<TKey, T> : IRepository<TKey, T> where T:class
|
|
{
|
|
private readonly DbContext _context;
|
|
|
|
public RepositoryBase(DbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public void AddRange(IEnumerable<T> entities)
|
|
{
|
|
_context.AddRange(entities);
|
|
}
|
|
public void Create(T entity)
|
|
{
|
|
_context.Add(entity);
|
|
}
|
|
|
|
public async Task CreateAsync(T entity)
|
|
{
|
|
await _context.AddAsync(entity);
|
|
}
|
|
|
|
|
|
public bool ExistsIgnoreQueryFilter(Expression<Func<T, bool>> expression)
|
|
{
|
|
return _context.Set<T>().IgnoreQueryFilters().Any(expression);
|
|
}
|
|
|
|
public bool Exists(Expression<Func<T, bool>> expression)
|
|
{
|
|
return _context.Set<T>().Any(expression);
|
|
}
|
|
|
|
public T Get(TKey id)
|
|
{
|
|
return _context.Find<T>(id);
|
|
}
|
|
|
|
public List<T> Get()
|
|
{
|
|
return _context.Set<T>().ToList();
|
|
}
|
|
|
|
public async Task<List<T>> GetListByIdList(List<TKey> ids)
|
|
{
|
|
return await _context.Set<T>().Where(e => ids.Contains(EF.Property<TKey>(e, "id"))).ToListAsync();
|
|
}
|
|
public void Remove(T entity)
|
|
{
|
|
_context.Set<T>().Remove(entity);
|
|
}
|
|
|
|
public void RemoveRange(IEnumerable<T> entities)
|
|
{
|
|
_context.Set<T>().RemoveRange(entities);
|
|
}
|
|
public void SaveChanges()
|
|
{
|
|
_context.SaveChanges();
|
|
}
|
|
|
|
public async Task SaveChangesAsync()
|
|
{
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task<IDbContextTransaction> BeginTransactionAsync()
|
|
{
|
|
return await _context.Database.BeginTransactionAsync();
|
|
}
|
|
}
|
|
}
|