_context.savechanges() 不工作 asp.net 核心 3.1
_context.savechanges() not working asp.net core 3.1
在下面的代码中
BorrowHistoryTracking = await _context.BorrowHistoryTrackings.FindAsync(id);
BorrowHistoryTracking.ReturnDate = DateTime.Today.Date;
await _context.AddAsync(BorrowHistoryTracking);
_context.Entry(BorrowHistoryTracking).State = EntityState.Modified;
await _context.SaveChangesAsync();
我可以看到 BorrowHistoryTracking.ReturnDate = DateTime.Today.Date;
正确地修改了 属性 ReturnDate
。
但是将修改后的对象添加到上下文中并将状态设置为修改后不知何故不起作用。
await _context.SaveChangesAsync();
后属性ReturnDate
未修改。
知道吗,我做错了什么?
这里是BorrowHistoryTracking
class
public class BorrowHistoryTracking
{
public int BorrowHistoryTrackingId { get; set; }
[Required]
[Display(Name = "Book")]
public int BookId { get; set; }
public Book Book { get; set; }
[Required]
[Display(Name = "Customer")]
public int LibraryCustomerTrackingId { get; set; }
public LibraryCustomerTracking LibraryCustomerTracking { get; set; }
[Display(Name = "Borrow Date")]
[DataType(DataType.Date)]
public DateTime BorrowDate { get; set; }
[Display(Name = "Return Date")]
[DataType(DataType.Date)]
public DateTime? ReturnDate { get; set; }
[Required]
[Column(TypeName = "nvarchar(24)")]
[Display(Name = "Borrowing Options")]
public string BorrowingOptions { get; set; }
}
这是我的背景 class
public FilippaLibraryContext (DbContextOptions<FilippaLibraryContext> options)
: base(options)
{
}
public DbSet<Book> Books { get; set; }
public DbSet<LibraryCustomer> LibraryCustomers { get; set; }
public DbSet<BorrowHistory> BorrowHistories { get; set; }
public DbSet<BorrowHistoryTracking> BorrowHistoryTrackings { get; set; }
public DbSet<LibraryCustomerTracking> LibraryCustomerTrackings { get; set; }
}
在tableBorrowHistoryTracking
我想记录借书情况。它是 BorrowHistory
的一种副本,但也包含过去的预订。 BorrowHistory
仅包含已预订的内容,不保留过去事件的痕迹。
动作方法为
public async Task<IActionResult> OnPostAsync(int? id)
{
if (id == null)
{
return NotFound();
}
BorrowHistory = await _context.BorrowHistories.FindAsync(id);
LibraryCustomer = await _context.LibraryCustomers.FindAsync(BorrowHistory.LibraryCustomerId);
BorrowHistoryTracking = await _context.BorrowHistoryTrackings.FindAsync(id);
LibraryCustomerTracking = await _context.LibraryCustomerTrackings.FindAsync(BorrowHistoryTracking.LibraryCustomerTrackingId);
if (BorrowHistory != null)
{
_context.BorrowHistories.Remove(BorrowHistory);
await _context.SaveChangesAsync();
}
if (LibraryCustomer != null)
{
_context.LibraryCustomers.Remove(LibraryCustomer);
await _context.SaveChangesAsync();
}
if (BorrowHistoryTracking != null)
{
var BorrowHistoryTracking = await _context.BorrowHistoryTrackings.FindAsync(id);
BorrowHistoryTracking.ReturnDate = DateTime.Today.Date;
_context.Entry(BorrowHistoryTracking).State = EntityState.Modified;
await _context.SaveChangesAsync();
}
if (LibraryCustomerTracking != null)
{
await _context.SaveChangesAsync();
}
return RedirectToPage("../Books/Index");
}
此方法删除 BorrowHistory
table 中的预订,但应保留 BorrowHistoryTracking
table
中的预订痕迹
第二次更新
namespace Library.Pages.BorrowHistories
{
public class DeleteModel : PageModel
{
private readonly FilippaLibraryContext _context;
public DeleteModel(FilippaLibraryContext context)
{
_context = context;
}
[BindProperty]
public BorrowHistory BorrowHistory { get; set; }
[BindProperty]
public LibraryCustomer LibraryCustomer { get; set; }
public BorrowHistoryTracking BorrowHistoryTracking { get; set; }
public LibraryCustomerTracking LibraryCustomerTracking { get; set; }
public async Task<IActionResult> OnGetAsync(int? id)
{
if (id == null)
{
return NotFound();
}
BorrowHistory = await _context.BorrowHistories
.Include(b => b.Book)
.Include(b => b.LibraryCustomer).FirstOrDefaultAsync(m => m.BorrowHistoryId == id);
BorrowHistoryTracking = await _context.BorrowHistoryTrackings
.Include(b => b.Book)
.Include(b => b.LibraryCustomerTracking).FirstOrDefaultAsync(m => m.BorrowHistoryTrackingId == id);
if (BorrowHistory == null)
{
return NotFound();
}
return Page();
}
public async Task<IActionResult> OnPostAsync(int? id)
{
if (id == null)
{
return NotFound();
}
BorrowHistory = await _context.BorrowHistories.FindAsync(id);
LibraryCustomer = await _context.LibraryCustomers.FindAsync(BorrowHistory.LibraryCustomerId);
BorrowHistoryTracking = await _context.BorrowHistoryTrackings.FindAsync(id);
LibraryCustomerTracking = await _context.LibraryCustomerTrackings.FindAsync(BorrowHistoryTracking.LibraryCustomerTrackingId);
if (BorrowHistory != null)
{
_context.BorrowHistories.Remove(BorrowHistory);
await _context.SaveChangesAsync();
}
if (LibraryCustomer != null)
{
_context.LibraryCustomers.Remove(LibraryCustomer);
await _context.SaveChangesAsync();
}
if (BorrowHistoryTracking != null)
{
var BorrowHistoryTracking = await _context.BorrowHistoryTrackings.FindAsync(id);
BorrowHistoryTracking.ReturnDate = DateTime.Today.Date;
_context.Entry(BorrowHistoryTracking).State = EntityState.Modified;
await _context.SaveChangesAsync();
}
if (LibraryCustomerTracking != null)
{
await _context.SaveChangesAsync();
}
return RedirectToPage("../Books/Index");
}
}
}
这里是 BorrowHistory
class
public class BorrowHistory
{
public int BorrowHistoryId { get; set; }
[Required]
[Display(Name = "Book")]
public int BookId { get; set; }
public Book Book { get; set; }
[Required]
[Display(Name = "Customer")]
public int LibraryCustomerId { get; set; }
public LibraryCustomer LibraryCustomer { get; set; }
[Display(Name = "Borrow Date")]
[DataType(DataType.Date)]
public DateTime BorrowDate { get; set; }
[Display(Name = "Return Date")]
[DataType(DataType.Date)]
public DateTime? ReturnDate { get; set; }
[Required]
[Column(TypeName = "nvarchar(24)")]
[Display(Name = "Borrowing Options")]
public string BorrowingOptions { get; set; }
}
你有一个错误。删除 _context.AddAsync 并添加 EntityState.Modified
var existingTracking = await _context.BorrowHistoryTrackings.FindAsync(id);
//or if existingTracking ==null try this
var existingTracking = await
_context.BorrowHistoryTrackings.FirstOrDefaultAsync(i=> i.Id==id);
if (existingTracking != null)
{
existingTracking.ReturnDate = DateTime.Today.Date;
_context.Entry(existingTracking).State = EntityState.Modified;
await _context.SaveChangesAsync();
}
else .... return errror
更新
从您的更新中我可以看出您正在尝试使用相同的 ID 来查找 BorrowHistory 和 BorrowHistoryTracking。恕我直言,这也是一个错误。你必须通过 id 找到一个。在此之后,您也许应该使用 BookId(或类似的东西)来查找另一条记录。
var bookId BorrowHistoryTracking.BookId;
var existingTracking = await
_context.BorrowHistoryTrackings.FirstOrDefaultAsync(i=> i.BookId==bookId);
.... and so on
解决方案
我找到了解决办法。
我已经使用 DBCC CHECKIDENT ('table_name', RESEED, 1)
重新播种了我的数据库表
通过这种方式,我确保 BorrowHistory
和 BorrowHistoryTracking
表的 ID 相同。
指向同一行,以下代码将完成工作并将 ReturnDate
设置为正确的值:
BorrowHistoryTracking.ReturnDate = DateTime.Today.Date;
_context.Attach(BorrowHistoryTracking).State = EntityState.Modified;
在下面的代码中
BorrowHistoryTracking = await _context.BorrowHistoryTrackings.FindAsync(id);
BorrowHistoryTracking.ReturnDate = DateTime.Today.Date;
await _context.AddAsync(BorrowHistoryTracking);
_context.Entry(BorrowHistoryTracking).State = EntityState.Modified;
await _context.SaveChangesAsync();
我可以看到 BorrowHistoryTracking.ReturnDate = DateTime.Today.Date;
正确地修改了 属性 ReturnDate
。
但是将修改后的对象添加到上下文中并将状态设置为修改后不知何故不起作用。
await _context.SaveChangesAsync();
后属性ReturnDate
未修改。
知道吗,我做错了什么?
这里是BorrowHistoryTracking
class
public class BorrowHistoryTracking
{
public int BorrowHistoryTrackingId { get; set; }
[Required]
[Display(Name = "Book")]
public int BookId { get; set; }
public Book Book { get; set; }
[Required]
[Display(Name = "Customer")]
public int LibraryCustomerTrackingId { get; set; }
public LibraryCustomerTracking LibraryCustomerTracking { get; set; }
[Display(Name = "Borrow Date")]
[DataType(DataType.Date)]
public DateTime BorrowDate { get; set; }
[Display(Name = "Return Date")]
[DataType(DataType.Date)]
public DateTime? ReturnDate { get; set; }
[Required]
[Column(TypeName = "nvarchar(24)")]
[Display(Name = "Borrowing Options")]
public string BorrowingOptions { get; set; }
}
这是我的背景 class
public FilippaLibraryContext (DbContextOptions<FilippaLibraryContext> options)
: base(options)
{
}
public DbSet<Book> Books { get; set; }
public DbSet<LibraryCustomer> LibraryCustomers { get; set; }
public DbSet<BorrowHistory> BorrowHistories { get; set; }
public DbSet<BorrowHistoryTracking> BorrowHistoryTrackings { get; set; }
public DbSet<LibraryCustomerTracking> LibraryCustomerTrackings { get; set; }
}
在tableBorrowHistoryTracking
我想记录借书情况。它是 BorrowHistory
的一种副本,但也包含过去的预订。 BorrowHistory
仅包含已预订的内容,不保留过去事件的痕迹。
动作方法为
public async Task<IActionResult> OnPostAsync(int? id)
{
if (id == null)
{
return NotFound();
}
BorrowHistory = await _context.BorrowHistories.FindAsync(id);
LibraryCustomer = await _context.LibraryCustomers.FindAsync(BorrowHistory.LibraryCustomerId);
BorrowHistoryTracking = await _context.BorrowHistoryTrackings.FindAsync(id);
LibraryCustomerTracking = await _context.LibraryCustomerTrackings.FindAsync(BorrowHistoryTracking.LibraryCustomerTrackingId);
if (BorrowHistory != null)
{
_context.BorrowHistories.Remove(BorrowHistory);
await _context.SaveChangesAsync();
}
if (LibraryCustomer != null)
{
_context.LibraryCustomers.Remove(LibraryCustomer);
await _context.SaveChangesAsync();
}
if (BorrowHistoryTracking != null)
{
var BorrowHistoryTracking = await _context.BorrowHistoryTrackings.FindAsync(id);
BorrowHistoryTracking.ReturnDate = DateTime.Today.Date;
_context.Entry(BorrowHistoryTracking).State = EntityState.Modified;
await _context.SaveChangesAsync();
}
if (LibraryCustomerTracking != null)
{
await _context.SaveChangesAsync();
}
return RedirectToPage("../Books/Index");
}
此方法删除 BorrowHistory
table 中的预订,但应保留 BorrowHistoryTracking
table
第二次更新
namespace Library.Pages.BorrowHistories
{
public class DeleteModel : PageModel
{
private readonly FilippaLibraryContext _context;
public DeleteModel(FilippaLibraryContext context)
{
_context = context;
}
[BindProperty]
public BorrowHistory BorrowHistory { get; set; }
[BindProperty]
public LibraryCustomer LibraryCustomer { get; set; }
public BorrowHistoryTracking BorrowHistoryTracking { get; set; }
public LibraryCustomerTracking LibraryCustomerTracking { get; set; }
public async Task<IActionResult> OnGetAsync(int? id)
{
if (id == null)
{
return NotFound();
}
BorrowHistory = await _context.BorrowHistories
.Include(b => b.Book)
.Include(b => b.LibraryCustomer).FirstOrDefaultAsync(m => m.BorrowHistoryId == id);
BorrowHistoryTracking = await _context.BorrowHistoryTrackings
.Include(b => b.Book)
.Include(b => b.LibraryCustomerTracking).FirstOrDefaultAsync(m => m.BorrowHistoryTrackingId == id);
if (BorrowHistory == null)
{
return NotFound();
}
return Page();
}
public async Task<IActionResult> OnPostAsync(int? id)
{
if (id == null)
{
return NotFound();
}
BorrowHistory = await _context.BorrowHistories.FindAsync(id);
LibraryCustomer = await _context.LibraryCustomers.FindAsync(BorrowHistory.LibraryCustomerId);
BorrowHistoryTracking = await _context.BorrowHistoryTrackings.FindAsync(id);
LibraryCustomerTracking = await _context.LibraryCustomerTrackings.FindAsync(BorrowHistoryTracking.LibraryCustomerTrackingId);
if (BorrowHistory != null)
{
_context.BorrowHistories.Remove(BorrowHistory);
await _context.SaveChangesAsync();
}
if (LibraryCustomer != null)
{
_context.LibraryCustomers.Remove(LibraryCustomer);
await _context.SaveChangesAsync();
}
if (BorrowHistoryTracking != null)
{
var BorrowHistoryTracking = await _context.BorrowHistoryTrackings.FindAsync(id);
BorrowHistoryTracking.ReturnDate = DateTime.Today.Date;
_context.Entry(BorrowHistoryTracking).State = EntityState.Modified;
await _context.SaveChangesAsync();
}
if (LibraryCustomerTracking != null)
{
await _context.SaveChangesAsync();
}
return RedirectToPage("../Books/Index");
}
}
}
这里是 BorrowHistory
class
public class BorrowHistory
{
public int BorrowHistoryId { get; set; }
[Required]
[Display(Name = "Book")]
public int BookId { get; set; }
public Book Book { get; set; }
[Required]
[Display(Name = "Customer")]
public int LibraryCustomerId { get; set; }
public LibraryCustomer LibraryCustomer { get; set; }
[Display(Name = "Borrow Date")]
[DataType(DataType.Date)]
public DateTime BorrowDate { get; set; }
[Display(Name = "Return Date")]
[DataType(DataType.Date)]
public DateTime? ReturnDate { get; set; }
[Required]
[Column(TypeName = "nvarchar(24)")]
[Display(Name = "Borrowing Options")]
public string BorrowingOptions { get; set; }
}
你有一个错误。删除 _context.AddAsync 并添加 EntityState.Modified
var existingTracking = await _context.BorrowHistoryTrackings.FindAsync(id);
//or if existingTracking ==null try this
var existingTracking = await
_context.BorrowHistoryTrackings.FirstOrDefaultAsync(i=> i.Id==id);
if (existingTracking != null)
{
existingTracking.ReturnDate = DateTime.Today.Date;
_context.Entry(existingTracking).State = EntityState.Modified;
await _context.SaveChangesAsync();
}
else .... return errror
更新
从您的更新中我可以看出您正在尝试使用相同的 ID 来查找 BorrowHistory 和 BorrowHistoryTracking。恕我直言,这也是一个错误。你必须通过 id 找到一个。在此之后,您也许应该使用 BookId(或类似的东西)来查找另一条记录。
var bookId BorrowHistoryTracking.BookId;
var existingTracking = await
_context.BorrowHistoryTrackings.FirstOrDefaultAsync(i=> i.BookId==bookId);
.... and so on
解决方案
我找到了解决办法。
我已经使用 DBCC CHECKIDENT ('table_name', RESEED, 1)
通过这种方式,我确保 BorrowHistory
和 BorrowHistoryTracking
表的 ID 相同。
指向同一行,以下代码将完成工作并将 ReturnDate
设置为正确的值:
BorrowHistoryTracking.ReturnDate = DateTime.Today.Date;
_context.Attach(BorrowHistoryTracking).State = EntityState.Modified;