|
@@ -18,69 +18,69 @@ namespace Business.EntityFrameworkCore.SqlRepositories
|
|
|
{
|
|
{
|
|
|
public class SqlRepository<T>:ISqlRepository<T> where T : class, new()
|
|
public class SqlRepository<T>:ISqlRepository<T> where T : class, new()
|
|
|
{
|
|
{
|
|
|
- private IDbContextProvider<BusinessDbContext> _dbContextProvider;
|
|
|
|
|
|
|
+ readonly BusinessDbContext _dbContext;
|
|
|
|
|
|
|
|
- public SqlRepository(IDbContextProvider<BusinessDbContext> dbContextProvider)
|
|
|
|
|
|
|
+ public SqlRepository(BusinessDbContext dbContext)
|
|
|
{
|
|
{
|
|
|
- _dbContextProvider = dbContextProvider;
|
|
|
|
|
|
|
+ _dbContext = dbContext;
|
|
|
}
|
|
}
|
|
|
public async Task<int> Insert(T entity)
|
|
public async Task<int> Insert(T entity)
|
|
|
{
|
|
{
|
|
|
- _dbContextProvider.GetDbContext().Set<T>().Add(entity);
|
|
|
|
|
- return await _dbContextProvider.GetDbContext().SaveChangesAsync();
|
|
|
|
|
|
|
+ _dbContext.Set<T>().Add(entity);
|
|
|
|
|
+ return await _dbContext.SaveChangesAsync();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public async Task<int> Insert(List<T> entitylist)
|
|
public async Task<int> Insert(List<T> entitylist)
|
|
|
{
|
|
{
|
|
|
- _dbContextProvider.GetDbContext().Set<T>().AddRange(entitylist);
|
|
|
|
|
- return await _dbContextProvider.GetDbContext().SaveChangesAsync();
|
|
|
|
|
|
|
+ _dbContext.Set<T>().AddRange(entitylist);
|
|
|
|
|
+ return await _dbContext.SaveChangesAsync();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public async Task<int> Update(T entity)
|
|
public async Task<int> Update(T entity)
|
|
|
{
|
|
{
|
|
|
- _dbContextProvider.GetDbContext().Set<T>().Update(entity);
|
|
|
|
|
- return await _dbContextProvider.GetDbContext().SaveChangesAsync();
|
|
|
|
|
|
|
+ _dbContext.Set<T>().Update(entity);
|
|
|
|
|
+ return await _dbContext.SaveChangesAsync();
|
|
|
}
|
|
}
|
|
|
public async Task<int> Update(List<T> entitylist)
|
|
public async Task<int> Update(List<T> entitylist)
|
|
|
{
|
|
{
|
|
|
- _dbContextProvider.GetDbContext().Set<T>().UpdateRange(entitylist);
|
|
|
|
|
- return await _dbContextProvider.GetDbContext().SaveChangesAsync();
|
|
|
|
|
|
|
+ _dbContext.Set<T>().UpdateRange(entitylist);
|
|
|
|
|
+ return await _dbContext.SaveChangesAsync();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public async Task<int> Update(Expression<Func<T, bool>> whereLambda, Expression<Func<T, T>> entity)
|
|
public async Task<int> Update(Expression<Func<T, bool>> whereLambda, Expression<Func<T, T>> entity)
|
|
|
{
|
|
{
|
|
|
- _dbContextProvider.GetDbContext().Set<T>().Where(whereLambda).BatchUpdate(entity);
|
|
|
|
|
- return await _dbContextProvider.GetDbContext().SaveChangesAsync();
|
|
|
|
|
|
|
+ _dbContext.Set<T>().Where(whereLambda).BatchUpdate(entity);
|
|
|
|
|
+ return await _dbContext.SaveChangesAsync();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public async Task<int> Delete(Expression<Func<T, bool>> whereLambda)
|
|
public async Task<int> Delete(Expression<Func<T, bool>> whereLambda)
|
|
|
{
|
|
{
|
|
|
- _dbContextProvider.GetDbContext().Set<T>().Where(whereLambda).BatchDelete();
|
|
|
|
|
- return await _dbContextProvider.GetDbContext().SaveChangesAsync();
|
|
|
|
|
|
|
+ _dbContext.Set<T>().Where(whereLambda).BatchDelete();
|
|
|
|
|
+ return await _dbContext.SaveChangesAsync();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public async Task<bool> IsExist(Expression<Func<T, bool>> whereLambda)
|
|
public async Task<bool> IsExist(Expression<Func<T, bool>> whereLambda)
|
|
|
{
|
|
{
|
|
|
- return await _dbContextProvider.GetDbContext().Set<T>().AnyAsync(whereLambda);
|
|
|
|
|
|
|
+ return await _dbContext.Set<T>().AnyAsync(whereLambda);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public async Task<List<T>> Select()
|
|
public async Task<List<T>> Select()
|
|
|
{
|
|
{
|
|
|
- return await _dbContextProvider.GetDbContext().Set<T>().ToListAsync();
|
|
|
|
|
|
|
+ return await _dbContext.Set<T>().ToListAsync();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public async Task<List<T>> Select(Expression<Func<T, bool>> whereLambda)
|
|
public async Task<List<T>> Select(Expression<Func<T, bool>> whereLambda)
|
|
|
{
|
|
{
|
|
|
- return await _dbContextProvider.GetDbContext().Set<T>().Where(whereLambda).ToListAsync();
|
|
|
|
|
|
|
+ return await _dbContext.Set<T>().Where(whereLambda).ToListAsync();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public async Task<Tuple<List<T>, int>> Select<S>(int pageSize, int pageIndex, Expression<Func<T, bool>> whereLambda, Expression<Func<T, S>> orderByLambda, bool isAsc)
|
|
public async Task<Tuple<List<T>, int>> Select<S>(int pageSize, int pageIndex, Expression<Func<T, bool>> whereLambda, Expression<Func<T, S>> orderByLambda, bool isAsc)
|
|
|
{
|
|
{
|
|
|
- var total = _dbContextProvider.GetDbContext().Set<T>().Where(whereLambda).Count();
|
|
|
|
|
|
|
+ var total = _dbContext.Set<T>().Where(whereLambda).Count();
|
|
|
|
|
|
|
|
if (isAsc)
|
|
if (isAsc)
|
|
|
{
|
|
{
|
|
|
- var entities = await _dbContextProvider.GetDbContext().Set<T>().Where(whereLambda)
|
|
|
|
|
|
|
+ var entities = await _dbContext.Set<T>().Where(whereLambda)
|
|
|
.OrderBy<T, S>(orderByLambda)
|
|
.OrderBy<T, S>(orderByLambda)
|
|
|
.Skip(pageSize * (pageIndex - 1))
|
|
.Skip(pageSize * (pageIndex - 1))
|
|
|
.Take(pageSize).ToListAsync();
|
|
.Take(pageSize).ToListAsync();
|
|
@@ -89,7 +89,7 @@ namespace Business.EntityFrameworkCore.SqlRepositories
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
- var entities =await _dbContextProvider.GetDbContext().Set<T>().Where(whereLambda)
|
|
|
|
|
|
|
+ var entities =await _dbContext.Set<T>().Where(whereLambda)
|
|
|
.OrderByDescending<T, S>(orderByLambda)
|
|
.OrderByDescending<T, S>(orderByLambda)
|
|
|
.Skip(pageSize * (pageIndex - 1))
|
|
.Skip(pageSize * (pageIndex - 1))
|
|
|
.Take(pageSize).ToListAsync();
|
|
.Take(pageSize).ToListAsync();
|
|
@@ -103,7 +103,7 @@ namespace Business.EntityFrameworkCore.SqlRepositories
|
|
|
DataSet ds = new DataSet();
|
|
DataSet ds = new DataSet();
|
|
|
try
|
|
try
|
|
|
{
|
|
{
|
|
|
- string connectionString = _dbContextProvider.GetDbContextAsync().Result.Database.GetDbConnection().ConnectionString;
|
|
|
|
|
|
|
+ string connectionString = _dbContext.Database.GetConnectionString();
|
|
|
SqlDataAdapter da = new SqlDataAdapter(sql, connectionString);
|
|
SqlDataAdapter da = new SqlDataAdapter(sql, connectionString);
|
|
|
da.Fill(ds, tableName);
|
|
da.Fill(ds, tableName);
|
|
|
}
|
|
}
|