GenericRepository Sample No. 2

  1. public class GenericRepo : IGeneric<Student, int>,IDisposable  
  2.    {  
  3.        private StudentContext db;  
  4.        public GenericRepo()  
  5.        {  
  6.            this.db = new StudentContext();  
  7.        }  
  8.        public List<Student> SelectAll()  
  9.        {  
  10.            return db.Students.ToList();  
  11.        }  
  12.        public Student SelectByID(int id)  
  13.        {  
  14.            return db.Students.Where(x => x.StId == id).SingleOrDefault();  
  15.        }  
  16.         
  17.        public void Insert(Student obj)  
  18.        {  
  19.            db.Students.Add(obj);  
  20.        }  
  21.        public void Update(Student obj)  
  22.        {  
  23.            db.Entry(obj).State = System.Data.Entity.EntityState.Modified;  
  24.        }  
  25.        public void Delete(int id)  
  26.        {  
  27.            Student obj = db.Students.Where(x => x.StId == id).SingleOrDefault();  
  28.            db.Students.Remove(obj);  
  29.        }  
  30.        public void Save()  
  31.        {  
  32.            db.SaveChanges();  
  33.        }  
  34.        public void Dispose()  
  35.        {  
  36.            db.Dispose();  
  37.        }  
  38.        
  39.    }  

Comments

Popular posts from this blog

IGenricRepository

Summary of All Data Source Patterns