90 lines
2.4 KiB
C#
90 lines
2.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Hncore.Infrastructure.DDD;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
|
|
|
namespace Hncore.Infrastructure.EF
|
|
{
|
|
public class RepositoryBase<TEntity, TId> : IRepository<TEntity, TId>
|
|
where TEntity : Entity<TId>
|
|
{
|
|
protected DbContext Dbcontext;
|
|
|
|
public RepositoryBase(IRepositoryDbContext dbContext)
|
|
{
|
|
Dbcontext = dbContext.DbContext;
|
|
}
|
|
|
|
public TEntity FindById(TId id)
|
|
{
|
|
return Dbcontext.Set<TEntity>().Find(id);
|
|
}
|
|
|
|
public Task<TEntity> FindByIdAsync(TId id)
|
|
{
|
|
return Dbcontext.Set<TEntity>().FindAsync(id);
|
|
}
|
|
|
|
public void Add(TEntity entity)
|
|
{
|
|
Dbcontext.Set<TEntity>().Add(entity);
|
|
}
|
|
|
|
|
|
public Task AddAsync(TEntity entity)
|
|
{
|
|
return Dbcontext.Set<TEntity>().AddAsync(entity);
|
|
}
|
|
/// <summary>
|
|
/// 批量添加
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
public void AddRange(List<TEntity> entity)
|
|
{
|
|
Dbcontext.Set<TEntity>().AddRange(entity);
|
|
}
|
|
/// <summary>
|
|
/// 批量添加
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
public Task AddRangeAsync(List<TEntity> entity)
|
|
{
|
|
return Dbcontext.Set<TEntity>().AddRangeAsync(entity);
|
|
}
|
|
/// <summary>
|
|
/// 批量修改
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
public void UpdateRange(List<TEntity> entity)
|
|
{
|
|
Dbcontext.Set<TEntity>().UpdateRange(entity);
|
|
}
|
|
/// <summary>
|
|
/// 批量删除
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
public void RemoveRange(List<TEntity> entity)
|
|
{
|
|
Dbcontext.Set<TEntity>().RemoveRange(entity);
|
|
}
|
|
|
|
public void Remove(TEntity entity)
|
|
{
|
|
Dbcontext.Set<TEntity>().Remove(entity);
|
|
}
|
|
public void Update(TEntity entity)
|
|
{
|
|
Dbcontext.Set<TEntity>().Update(entity);
|
|
}
|
|
|
|
public IQueryable<TEntity> GetQueryable()
|
|
{
|
|
return Dbcontext.Set<TEntity>();
|
|
}
|
|
|
|
}
|
|
} |