82 lines
2.2 KiB
C#
82 lines
2.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Linq.Expressions;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using ViraScraper.Models;
|
|
|
|
namespace ViraScraper.Repositories
|
|
{
|
|
public class RepositoryBase<T> : IRepositoryBase<T> where T : class
|
|
{
|
|
protected readonly ScrapDbContext _context;
|
|
|
|
public RepositoryBase(ScrapDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
public IQueryable<T> GetAll()
|
|
{
|
|
return _context.Set<T>().AsNoTracking();
|
|
}
|
|
|
|
public IQueryable<T> GetAll(Expression<Func<T, bool>> expression)
|
|
{
|
|
return _context.Set<T>().Where(expression).AsNoTracking();
|
|
}
|
|
|
|
public async void Insert(T entity)
|
|
{
|
|
await _context.AddAsync(entity);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async void InsertRang(IEnumerable<T> entitys)
|
|
{
|
|
await _context.AddRangeAsync(entitys);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async void InsertS(T entity)
|
|
{
|
|
var ent = _context.Entry(entity);
|
|
if (ent == null)
|
|
{
|
|
await _context.AddAsync(entity);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
}
|
|
|
|
public async void InsertSRang(IEnumerable<T> entitys)
|
|
{
|
|
foreach (var entity in entitys)
|
|
{
|
|
var ent = _context.Set<T>().AsNoTracking().FirstOrDefault(e => e.Equals(entity));
|
|
if (ent == null)
|
|
{
|
|
await _context.AddAsync(entity);
|
|
}
|
|
}
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async void Remove(T entity)
|
|
{
|
|
var ent = _context.Entry(entity);
|
|
ent.State = EntityState.Detached;
|
|
ent.State = EntityState.Deleted;
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async void Update(T entity)
|
|
{
|
|
var ent = _context.Entry(entity);
|
|
ent.State = EntityState.Detached;
|
|
ent.State = EntityState.Modified;
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
}
|
|
}
|