forked from linezero/NETCoreBBS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepository.cs
More file actions
55 lines (47 loc) · 1.35 KB
/
Copy pathRepository.cs
File metadata and controls
55 lines (47 loc) · 1.35 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
using Microsoft.EntityFrameworkCore;
using NetCoreBBS.Entities;
using NetCoreBBS.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NetCoreBBS.Infrastructure.Repositorys
{
public class Repository<T> : IRepository<T> where T : BaseEntity
{
private readonly DataContext _dbContext;
public Repository(DataContext dbContext)
{
_dbContext = dbContext;
}
public virtual T GetById(int id)
{
return _dbContext.Set<T>().Find(id);
}
public virtual IEnumerable<T> List()
{
return _dbContext.Set<T>().AsEnumerable();
}
public virtual IEnumerable<T> List(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
{
return _dbContext.Set<T>()
.Where(predicate)
.AsEnumerable();
}
public void Add(T entity)
{
_dbContext.Set<T>().Add(entity);
_dbContext.SaveChanges();
}
public void Edit(T entity)
{
_dbContext.Entry(entity).State = EntityState.Modified;
_dbContext.SaveChanges();
}
public void Delete(T entity)
{
_dbContext.Set<T>().Remove(entity);
_dbContext.SaveChanges();
}
}
}