title: How to update data in sql using mvc?
body: <p>I have 2 tables, <code>Table_Books</code> and <code>Table_RentedBooks</code>. When a user rents a book, I created a row in <code>Table_Rented</code> books with a <code>IsDelete = 0</code> (it means it is still rented).</p>
  <p>When user decides to deliver the book I want to change the column as <code>IsDelete=1</code> but I get this error</p>
  <blockquote>
  <p>Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded</p>
  </blockquote>
  <p>I tried to do everything but still can't make it work. What should I change in my tables and codes?</p>
  <p><strong>Table_Books</strong></p>
  <pre><code>BookID  int Unchecked,
  CategoryID int  Unchecked,
  BookName varchar(100)   Unchecked,
  Writer varchar(50)  Unchecked,
  Page int    Unchecked,
  Recorddate date Unchecked,
  UpdateDate date Checked,
  Image varchar(300)  Unchecked,
  Subject varchar(500)    Unchecked,
  NumberofBooks int   Unchecked,
  IsDeleted int   Checked
  (BookID is the primary key)
  </code></pre>
  <p><strong>Table_RentedBooks</strong></p>
  <pre><code>RentID  int Unchecked,
  BookID  int Unchecked,
  UserID  int Unchecked,
  Recorddate date Unchecked,
  Deliverydate    Unchecked,
  IsDeleted   int Unchecked,
  (Recorddate is the primary key) 
  </code></pre>
  <p><em>RENT BOOK</em></p>
  <pre><code>[HttpPost]
  public ActionResult RentBook(Table_Books book)
  {
          User data = Session[&quot;useriz&quot;] as User ;
      
          Book bookdata= Session[&quot;bookdata&quot;] as Book;
  
          Table_RentedBooks z = new Table_RentedBooks ();
          var tbookid = book.BookID;
          var numberofbooks1=(int)bookdata.NumberofBooks -1;
  
          book.CategoryID = kitapdata.KategoriID;
          book.Image= kitapdata.KitapGorsel;
          book.NumberofBooks = numberofbooks1;
  
          bookrepo.Update(book);
              
          DateTime date = DateTime.Now;
          DateTime Recorddate = date;
          DateTime Deliverydate = date.AddDays(14);
          z.IsDeleted = 0;
          z.Deliverydate = Deliverydate   ;
          z.Recorddate = Recorddate ;
          z.UserID = data.UserID ;
          z.BookID = tbookid;
  
          Random rnd = new Random();
          int randomint = rnd.Next();
          z.RentID= randomint;
  
          rentedrepo.Add(z);
        
          return RedirectToAction(&quot;index&quot;, &quot;Home&quot;);
      }
  }
  </code></pre>
  <p><em>DELIVER BOOK</em> (This method is the problem)</p>
  <pre><code>[HttpPost]
  public ActionResult KitapTeslimEt(Table_Book book,Table_RentedBooks rentedbook)
  {
          User data = Session[&quot;useriz&quot;] as User ;
          
      
          Book bookdata= Session[&quot;bookdata&quot;] as Book;
  
                Table_RentedBooks z = new Table_RentedBooks ();
               var tbookid = book.BookID;
              var numberofbooks1=(int)bookdata.NumberofBooks -1;
              book.CategoryID = kitapdata.KategoriID;
              book.Image= kitapdata.KitapGorsel;
              book.NumberofBooks = numberofbooks1;
              bookrepo.Update(book);
              
          DateTime date= DateTime.Now;
          DateTime Recorddate = date;
          DateTime Deliverydate   = date.AddDays(14);
              z.IsDeleted = 1;
              z.Deliverydate   = Deliverydate ;
              z.Recorddate = Recorddate ;
              z.UserID = data.UserID ;
              z.BookID= tbookid;
              Random rnd = new Random();
              int randomint = rnd.Next();
              z.RentID= randomint;
             rentedrepo.Update(z);
              
              return RedirectToAction(&quot;index&quot;, &quot;Home&quot;);
          
      }
  </code></pre>
  <pre><code>
  UPDATE METHOD
  </code></pre>
  <p>public void Update(T par)
  {</p>
  <pre><code>        try
          {
              db.SaveChanges();
          }
          catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
          {
              Exception raise = dbEx;
              foreach (var validationErrors in dbEx.EntityValidationErrors)
              {
                  foreach (var validationError in validationErrors.ValidationErrors)
                  {
                      string message = string.Format(&quot;{0}:{1}&quot;,
                          validationErrors.Entry.Entity.ToString(),
                          validationError.ErrorMessage);
                      // raise a new exception nesting
                      // the current instance as InnerException
                      raise = new InvalidOperationException(message, raise);
                  }
              }
              throw raise;
          }
      }
  </code></pre>
  <pre><code>    
  
  
  </code></pre>