| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using ConsoleHttp.Model;
- using MqttMsgServer.Model;
- namespace MqttMsgServer.Dao
- {
- public class DbRepository<TPrimaryKey, T> : IRepository<TPrimaryKey, T> where T : Entity<TPrimaryKey>
- {
- private readonly ApplicationDbContext _dbContext;
- //public DbRepository(ApplicationDbContext dbContext)
- //{
- // _dbContext = dbContext;
- //}
- public DbRepository()
- {
- _dbContext = new ApplicationDbContext();
- }
- public IEnumerable<T> GetAll()
- {
- return _dbContext.Set<T>().Where(i => true);
- }
- public T GetById(TPrimaryKey id)
- {
- return _dbContext.Set<T>().AsNoTracking().FirstOrDefault(i => i.Id.Equals(id));
- }
- public T Add(T t)
- {
- _dbContext.Set<T>().Add(t);
- if (_dbContext.SaveChanges() > 0)
- {
- return t;
- }
- return null;
- }
- public T Update(T t)
- {
- _dbContext.Set<T>().Attach(t);
- if (_dbContext.SaveChanges() > 0)
- {
- return t;
- }
- return null;
- }
- public T Delete(T t)
- {
- _dbContext.Set<T>().Remove(t);
- if (_dbContext.SaveChanges() > 0)
- {
- return t;
- }
- return null;
- }
- }
- }
|