コード例 #1
0
 public virtual IList <T> FetchAll()
 {
     using (PSBlogContext db = new PSBlogContext())
     {
         return(db.Set <T>().ToList());
     }
 }
コード例 #2
0
 public virtual T FindById(int id)
 {
     using (PSBlogContext db = new PSBlogContext())
     {
         return(db.Set <T>().First(el => el.Id == id));
     }
 }
コード例 #3
0
 public virtual void Add(T entity)
 {
     using (PSBlogContext db = new PSBlogContext())
     {
         db.Set <T>().Add(entity);
         db.SaveChanges();
     }
 }
コード例 #4
0
 public virtual void Remove(int id)
 {
     using (PSBlogContext db = new PSBlogContext())
     {
         T entity = db.Set <T>().First(el => el.Id == id);
         db.Entry(entity).State = EntityState.Deleted;
         db.SaveChanges();
     }
 }
コード例 #5
0
 protected string GenerateUniqueSlug <T>(string urlSlug) where T : class, IUrlSlug
 {
     using (PSBlogContext db = new PSBlogContext())
     {
         bool unique = !db.Set <T>().Any(x => x.UrlSlug == urlSlug);
         if (!unique)
         {
             return(GenerateUniqueSlug <T>(urlSlug, 1));
         }
         return(urlSlug);
     }
 }
コード例 #6
0
 private string GenerateUniqueSlug <T>(string urlSlug, int numberTries) where T : class, IUrlSlug
 {
     using (PSBlogContext db = new PSBlogContext())
     {
         string newSlug = urlSlug + numberTries;
         bool   unique  = !db.Set <T>().Any(x => x.UrlSlug == newSlug);
         if (!unique)
         {
             newSlug = GenerateUniqueSlug <T>(newSlug, ++numberTries);
         }
         return(newSlug);
     }
 }