This repository was archived by the owner on Feb 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseRepository.cs
More file actions
185 lines (158 loc) · 5.71 KB
/
Copy pathBaseRepository.cs
File metadata and controls
185 lines (158 loc) · 5.71 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace VP_Core
{
public abstract class BaseRepository<Tentity, Tcontext> : IBaseRepository<Tentity, Tcontext>
where Tentity : class
where Tcontext : DbContext
{
protected readonly Tcontext context;
public BaseRepository(Tcontext context)
{
this.context = context;
}
public DbSet<Tentity> GetContext()
{
return GetContext<Tentity>();
}
public DbSet<TOEntity> GetContext<TOEntity>() where TOEntity : class
{
return context.Set<TOEntity>();
}
public IQueryable<Tentity> GetQuery(Expression<Func<Tentity, bool>> predicate = null)
{
return GetQuery<Tentity>(predicate);
}
public IQueryable<TOEntity> GetQuery<TOEntity>(Expression<Func<TOEntity, bool>> predicate = null) where TOEntity : class
{
if (predicate == null)
return context.Set<TOEntity>().Where(x => 1 == 1);
return context.Set<TOEntity>().Where(predicate);
}
public Tentity Get(Expression<Func<Tentity, bool>> predicate = null)
{
return Get<Tentity>(predicate);
}
public TOEntity Get<TOEntity>(Expression<Func<TOEntity, bool>> predicate = null) where TOEntity : class
{
if (predicate == null)
return context.Set<TOEntity>().Where(x => 1 == 1).AsNoTracking().FirstOrDefault();
return context.Set<TOEntity>().Where(predicate).AsNoTracking().FirstOrDefault();
}
public Tentity GetById(long Id)
{
return GetById(Id, false);
}
public Tentity GetById(long Id, bool AsNotracking)
{
return GetById<Tentity>(Id, AsNotracking);
}
public TOEntity GetById<TOEntity>(long Id) where TOEntity : class
{
return GetById<TOEntity>(Id, false);
}
public TOEntity GetById<TOEntity>(long Id, bool AsNotracking) where TOEntity : class
{
var entity = context.Set<TOEntity>().Find(Id);
if (entity != null && AsNotracking)
context.Entry(entity).State = EntityState.Detached;
return entity;
}
public void Insert(Tentity entity)
{
//context.Set<Tentity>().Add(entity);
//context.Add(entity);
Insert<Tentity>(entity);
}
public void Insert<TOEntity>(TOEntity entity) where TOEntity : class
{
//context.Set<TOEntity>().Add(entity);
context.Add(entity);
}
public void Update(Tentity entity)
{
//context.Entry(entity).State = EntityState.Modified;
//context.Set<Tentity>().Update(entity);
//context.Update(entity);
Update<Tentity>(entity);
}
public void Update<TOEntity>(TOEntity entity) where TOEntity : class
{
context.Entry(entity).State = EntityState.Modified;
//context.Set<TOEntity>().Update(entity);
context.Update(entity);
}
public void Update(Tentity entity, params string[] fields)
{
var attack = context.Attach(entity);
foreach (var field in fields)
{
if (entity.GetType().GetProperty(field) != null)//geçersiz bir
{
attack.Property(field).IsModified = true;
}
else
{
throw new Exception($"{field} is not a property of " + entity.GetType().Name);
}
}
}
public void Update<TOEntity>(TOEntity entity, params string[] fields) where TOEntity : class
{
var attack = context.Attach(entity);
foreach (var field in fields)
{
if (entity.GetType().GetProperty(field) != null)//geçersiz bir
{
attack.Property(field).IsModified = true;
}
else
{
throw new Exception($"{field} is not a property of " + entity.GetType().Name);
}
}
}
public void Delete(Tentity entity)
{
context.Entry(entity).State = EntityState.Deleted;
//context.Set<Tentity>().Remove(entity);
context.Remove(entity);
}
public int SaveChanges()
{
return context.SaveChanges();
}
public async Task<int> SaveChangesAsync()
{
return await context.SaveChangesAsync();
}
public void InsertRange(IEnumerable<Tentity> entities)
{
//context.Set<Tentity>().AddRange(entities);
//context.AddRange(entities);
InsertRange<Tentity>(entities);
}
public void InsertRange<TOEntity>(IEnumerable<TOEntity> entities) where TOEntity : class
{
context.Set<TOEntity>().AddRange(entities);
//context.AddRange(entities);
}
public void UpdateRange(IEnumerable<Tentity> entities)
{
context.Entry(entities).State = EntityState.Modified;
////context.Set<Tentity>().UpdateRange(entities);
//context.UpdateRange(entities);
}
public void DeleteRange(IEnumerable<Tentity> entities)
{
context.Entry(entities).State = EntityState.Deleted;
//context.Set<Tentity>().RemoveRange(entities);
context.RemoveRange(entities);
}
}
}