我想更新實體對象記錄,但它不顯示異常但它不更新記錄。實體框架Linq更新數據不起作用
在objetct以前它包含空值issue_detail,之後用戶更新數據調用下面的方法
public Boolean bookReturnedUpdate(Issue_Detail issue)
{
BookController bookController = new BookController();
int bookID = (int) issue.book_id;
Book_Detail book = bookController.findByBookID(bookID);
int noOfCopies = (int) book.no_of_copies;
book.no_of_copies = ++noOfCopies;
Console.Write("just before the update");
bookController.updateBook(book);
Console.WriteLine("Issue ID" +issue.issue_id);
Boolean status = false;
try
{
using (var db = new ModelDB())
{
DateTime issueD = (DateTime) issue.return_date;
Console.WriteLine("Details " + issueD); // here I can see the date is update and it contains the new date I have inserted
db.Entry(issue).State = EntityState.Modified;
db.Issue_Detail.Attach(issue);
db.SaveChanges(); // this returns 0 when I have checked wit h Console.WriteLine
status = true;
}
}
catch (Exception ex)
{
Console.WriteLine("Book return update error " + ex.InnerException);
}
return status;
}
謝謝。
我已經嘗試過db.SaveChanges()也沒有人若需要完整的資料庫,請檢查下面的鏈接Github上 https://github.com/ccmcwolf/LibraryManagementSystem
你爲什麼要叫'db.Issue_Detail.Attach(發行);以後'db.Entry(問題).STATE = EntityState.Modified';'? – grek40
我試着只是添加db.SaceChanges()它沒有工作,這就是爲什麼試圖添加也是 –