Guest User

Untitled

a guest
Jul 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Entity;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Forms;
  8.  
  9. namespace Mavtozavr.DataAccess
  10. {
  11. public class Repository
  12. {
  13. public static IQueryable<TEntity> FindAll<TEntity>()
  14. where TEntity : class
  15. {
  16. var context = new ListModelContainer();
  17. return context.Set<TEntity>();
  18. }
  19.  
  20. public static void Insert<TEntity>(TEntity entity)
  21. where TEntity : class
  22. {
  23. var context = new ListModelContainer();
  24.  
  25. context.Entry(entity).State = EntityState.Added;
  26. context.SaveChanges();
  27. }
  28.  
  29. public static void InsertMany<TEntity>(IEnumerable<TEntity> entities)
  30. where TEntity : class
  31. {
  32. var context = new ListModelContainer();
  33.  
  34. context.Configuration.AutoDetectChangesEnabled = false;
  35. context.Configuration.ValidateOnSaveEnabled = false;
  36. foreach (TEntity entity in entities)
  37. context.Entry(entity).State = EntityState.Added;
  38. context.SaveChanges();
  39. context.Configuration.AutoDetectChangesEnabled = true;
  40. context.Configuration.ValidateOnSaveEnabled = true;
  41. }
  42.  
  43. public static void Delete<TEntity>(TEntity entity)
  44. where TEntity : class
  45. {
  46. var contex = new ListModelContainer();
  47.  
  48. contex.Entry(entity).State = EntityState.Deleted;
  49. contex.SaveChanges();
  50. }
  51.  
  52. public static void DeleteMany<TEntity>(IEnumerable<TEntity> entities)
  53. where TEntity : class
  54. {
  55. var context = new ListModelContainer();
  56.  
  57. context.Configuration.AutoDetectChangesEnabled = false;
  58. context.Configuration.ValidateOnSaveEnabled = false;
  59. foreach (TEntity entity in entities)
  60. context.Entry(entity).State = EntityState.Deleted;
  61. context.SaveChanges();
  62. context.Configuration.AutoDetectChangesEnabled = true;
  63. context.Configuration.ValidateOnSaveEnabled = true;
  64. }
  65. }
  66. }
Add Comment
Please, Sign In to add comment