This repository was archived by the owner on Nov 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEfRepository.cs
More file actions
57 lines (48 loc) · 1.5 KB
/
Copy pathEfRepository.cs
File metadata and controls
57 lines (48 loc) · 1.5 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using WebNote.Core.Entities;
using WebNote.Core.Interfaces;
namespace WebNote.Infrastructure.Data
{
public class EfRepository<T> : IAsyncRepository<T> where T : BaseEntity
{
protected readonly AppDbContext _appDbContext;
public EfRepository(AppDbContext appDbContext)
{
_appDbContext = appDbContext;
}
public async Task<T> AddAsync(T entity)
{
await _appDbContext.Set<T>().AddAsync(entity);
await _appDbContext.SaveChangesAsync();
return entity;
}
public async Task<T> DeleteAsync(int? id)
{
var entity = await _appDbContext.Set<T>().FindAsync(id);
if (entity == null)
{
return entity;
}
_appDbContext.Set<T>().Remove(entity);
await _appDbContext.SaveChangesAsync();
return entity;
}
public async Task<T> GetByIdAsync(int? id)
{
return await _appDbContext.Set<T>().FindAsync(id);
}
public async Task<IReadOnlyList<T>> ListAllAsync()
{
return await _appDbContext.Set<T>().ToListAsync();
}
public async Task UpdateAsync(T entity)
{
_appDbContext.Entry(entity).State = EntityState.Modified;
await _appDbContext.SaveChangesAsync();
}
}
}