예제 #1
0
        public T Update(T entity)
        {
            var result = dbContext.Set <T>().Attach(entity);

            dbContext.Entry(entity).State = System.Data.Entity.EntityState.Modified; //xem lại
            dbContext.SaveChanges();
            return(result);
        }
예제 #2
0
 public ActionResult Edit([Bind(Include = "UserId,UserName,Password,Gender,Email,SecurityQuestion,SecurityAnswer,QQ,Tel,Address,PostCode,RegTime,LastLoginTime")] User user)
 {
     if (ModelState.IsValid)
     {
         db.Entry(user).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(user));
 }
예제 #3
0
 public ActionResult Edit([Bind(Include = "CategoryId,CategoryName")] Category category)
 {
     if (ModelState.IsValid)
     {
         db.Entry(category).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(category));
 }
예제 #4
0
 public ActionResult Edit([Bind(Include = "Id,Title,Producer,Date,Category,DEscription")] News news)
 {
     if (ModelState.IsValid)
     {
         db.Entry(news).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(news));
 }
예제 #5
0
 public ActionResult Edit([Bind(Include = "ID,Title,ReleaseDate,Text")] NewsViewModel newsViewModel)
 {
     if (ModelState.IsValid)
     {
         db.Entry(newsViewModel).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(newsViewModel));
 }
예제 #6
0
        public ActionResult Edit(Article article) //修改的时候提交的数据时一个类
        {
            if (ModelState.IsValid)
            {
                db.Entry(article).State = EntityState.Modified; // 修改
                db.SaveChanges();                               //保存
                return(RedirectToAction("index"));
            }

            return(View("index"));
        }
예제 #7
0
 public ActionResult Edit([Bind(Include = "Id,TimeStamp,Text, RowVersion")] NewsItem newsItem)
 {
     if (ModelState.IsValid)
     {
         try
         {
             db.Entry(newsItem).State = EntityState.Modified;
             db.SaveChanges();
             return(RedirectToAction("Index"));
         } catch (DbUpdateConcurrencyException ex)
         {
             ModelState.AddModelError(string.Empty,
                                      "Unable to save changes. The news was edited or deleted by another user.");
         }
     }
     return(View(newsItem));
 }
예제 #8
0
        private static void CatchConcurrencyConflict(NewsDBContext context, New loadedEntity)
        {
            Console.WriteLine("Text from DB: {0}", loadedEntity.Content);
            Console.WriteLine("Enter the corrected text:");
            string newEntity = Console.ReadLine();

            loadedEntity.Content = newEntity;

            try
            {
                context.SaveChanges();
                Console.WriteLine("Changes successfully saved in the DB.");
            }
            catch (DbUpdateConcurrencyException)
            {
                Console.WriteLine("Conflict!");
                context.Entry(loadedEntity).Reload();
                CatchConcurrencyConflict(context, loadedEntity);
            }
        }
예제 #9
0
        public async Task <IActionResult> PutNews([FromRoute] int id, [FromBody] News news)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != news.NewsId)
            {
                return(BadRequest());
            }

            //I need to get the date from the context

            //Datetime needs to be fixed.
            DateTime today = new DateTime();

            today = DateTime.Now;

            news.UpdatedDate = today;

            _context.Entry(news).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!NewsExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }