public virtual IList <T> FetchAll() { using (PSBlogContext db = new PSBlogContext()) { return(db.Set <T>().ToList()); } }
public virtual T FindById(int id) { using (PSBlogContext db = new PSBlogContext()) { return(db.Set <T>().First(el => el.Id == id)); } }
public virtual void Add(T entity) { using (PSBlogContext db = new PSBlogContext()) { db.Set <T>().Add(entity); db.SaveChanges(); } }
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(); } }
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); } }
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); } }