-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathReadOnlyRepository.cs
More file actions
40 lines (34 loc) · 1.17 KB
/
ReadOnlyRepository.cs
File metadata and controls
40 lines (34 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace HwProj.Repositories
{
public class ReadOnlyRepository<TEntity, TKey> : IReadOnlyRepository<TEntity, TKey>
where TEntity : class, IEntity<TKey>, new()
where TKey : IEquatable<TKey>
{
protected readonly DbContext Context;
public ReadOnlyRepository(DbContext context)
{
Context = context;
}
public IQueryable<TEntity> GetAll()
{
return Context.Set<TEntity>().AsNoTracking();
}
public IQueryable<TEntity> FindAll(Expression<Func<TEntity, bool>> predicate)
{
return Context.Set<TEntity>().AsNoTracking().Where(predicate);
}
public async Task<TEntity> GetAsync(TKey id)
{
return await Context.FindAsync<TEntity>(id).ConfigureAwait(false);
}
public virtual async Task<TEntity> FindAsync(Expression<Func<TEntity, bool>> predicate)
{
return await Context.Set<TEntity>().AsNoTracking().FirstOrDefaultAsync(predicate).ConfigureAwait(false);
}
}
}