初始提交
This commit is contained in:
90
Infrastructure/Hncore.Infrastructure/EF/RepositoryBase.cs
Normal file
90
Infrastructure/Hncore.Infrastructure/EF/RepositoryBase.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
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>();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user