Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. public class GenericRepository<T> : IGenericRepository<T> where T : class
  2. {
  3. public virtual T Add(T entity)
  4. {
  5. using (ApplicationDbContext _db = new ApplicationDbContext())
  6. {
  7. DbSet<T> _table = _db.Set<T>();
  8. using (var transaction = _db.Database.BeginTransaction())
  9. {
  10. try
  11. {
  12. var result = _table.Add(entity);
  13. transaction.Commit();
  14. return result;
  15. }
  16. catch
  17. {
  18. transaction.Rollback();
  19. return null;
  20. }
  21. }
  22. }
  23. }
  24.  
  25. // and so on for other CRUD methods...
  26. }
  27.  
  28. public class BuildingRespository : GenericRepository<Building>
  29. {
  30. public Building GetFullBuildingById(long Id)
  31. {
  32. using (ApplicationDbContext _db = new ApplicationDbContext())
  33. {
  34. return _db.Buildings.Include(x => x.BuildingClient).FirstOrDefault(x => x.Id == Id);
  35. }
  36. }
  37. // ...and so on for other entity class-specific requirements
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement