public void SaveNewsItem(NewsItem entity)
 {
     if (entity.Id == default)
     {
         _context.Entry(entity).State = EntityState.Added;
     }
     else
     {
         _context.Entry(entity).State = EntityState.Modified;
     }
     _context.SaveChanges();
 }
Example #2
0
 public void SaveTextField(TextField entity)
 {
     if (entity.Id == default)
     {
         _context.Entry(entity).State = EntityState.Added;
     }
     else
     {
         _context.Entry(entity).State = EntityState.Modified;
     }
     _context.SaveChanges();
 }
        public void gvCategories_UpdateItem(int id)
        {
            var context = new NewsSiteDbContext();
            Category item = context.Categories.Find(id);

            if (item == null)
            {
                ModelState.AddModelError("", String.Format("Item with id {0} was not found", id));
                return;
            }

            var editTextBox = this.gvCategories.Rows[this.gvCategories.EditIndex].Controls[0].Controls[0] as TextBox;

            if (editTextBox != null)
            {
                item.Name = editTextBox.Text;
            }

            TryUpdateModel(item);

            if (ModelState.IsValid)
            {
                context.SaveChanges();
            }
        }
        protected void btnInsert_Click(object sender, EventArgs e)
        {
            var categoryName = this.tbInsertCategory.Text;
            var categoryToInsert = new Category() { Name = categoryName };
            var context = new NewsSiteDbContext();

            context.Categories.Add(categoryToInsert);

            context.SaveChanges();
        }
        public void lvArticles_DeleteItem(int id)
        {
            var context = new NewsSiteDbContext();
            var article = context.Articles.Find(id);

            if (article != null)
            {
                context.Articles.Remove(article);
                context.SaveChanges();
            }
        }
Example #6
0
 public int SaveChanges()
 {
     try
     {
         return(_dbContext.SaveChanges());
     }
     catch (DbEntityValidationException ex)
     {
         _UndoChanges();
         throw ex;
     }
     catch (Exception ex)
     {
         _UndoChanges();
         throw ex;
     }
 }
        public void lvArticles_UpdateItem(int id)
        {
            var context = new NewsSiteDbContext();
            var item = context.Articles.Find(id);

            if (item == null)
            {
                ModelState.AddModelError("", String.Format("Item with id {0} was not found", id));
                return;
            }

            TryUpdateModel(item);

            if (ModelState.IsValid)
            {
                context.SaveChanges();
            }
        }
        public void gvCategories_DeleteItem(int id)
        {
            var context = new NewsSiteDbContext();
            var category = context.Categories.Find(id);

            if (category != null)
            {
                var articles = category.Articles.ToList();

                for (int i = 0; i < articles.Count; i++)
                {
                    context.Articles.Remove(articles[i]);
                }

                context.Categories.Remove(category);
            }

            context.SaveChanges();
        }
        public void lvArticles_InsertItem()
        {
            var context = new NewsSiteDbContext();
            var item = new Article();            

            TryUpdateModel(item);

            var authorId = Page.User.Identity.GetUserId();

            item.AuthorId = authorId;
            item.DateCreated = DateTime.Now;
            item.Likes = 0;

            if (ModelState.IsValid)
            {
                context.Articles.Add(item);

                context.SaveChanges();
            }
        }