Entity framework core _context.savechanges()无法使用asp.net core 3.1
在下面的代码中Entity framework core _context.savechanges()无法使用asp.net core 3.1,entity-framework-core,visual-studio-2019,asp.net-core-3.1,Entity Framework Core,Visual Studio 2019,Asp.net Core 3.1,在下面的代码中 BorrowHistoryTracking=wait\u context.BorrowHistoryTrackings.FindAsync(id); BorrowHistoryTracking.ReturnDate=DateTime.Today.Date; wait_context.AddAsync(借用历史跟踪); _context.Entry(借入历史跟踪).State=EntityState.Modified; wait_context.SaveChangesAsync()
BorrowHistoryTracking=wait\u context.BorrowHistoryTrackings.FindAsync(id);
BorrowHistoryTracking.ReturnDate=DateTime.Today.Date;
wait_context.AddAsync(借用历史跟踪);
_context.Entry(借入历史跟踪).State=EntityState.Modified;
wait_context.SaveChangesAsync();
我可以看到,BorrowHistoryTracking.ReturnDate=DateTime.Today.Date代码>正确修改属性ReturnDate
。
但是,将修改后的对象添加到上下文中并将状态设置为“修改”并不起作用
在wait_context.SaveChangesAsync()之后代码>不修改属性ReturnDate
知道我做错了什么吗
这里是借用历史跟踪
类
公共类借用历史跟踪
{
公共int BorrowHistoryTrackingId{get;set;}
[必需]
[显示(Name=“Book”)]
public int BookId{get;set;}
公共图书{get;set;}
[必需]
[显示(Name=“客户”)]
public int LibraryCustomerTrackingId{get;set;}
公共图书馆CustomerTracking图书馆CustomerTracking{get;set;}
[显示(Name=“借用日期”)]
[数据类型(DataType.Date)]
公共日期时间借用日期{get;set;}
[显示(Name=“返回日期”)]
[数据类型(DataType.Date)]
公共日期时间?返回日期{get;set;}
[必需]
[列(TypeName=“nvarchar(24)”)]
[显示(Name=“借用选项”)]
公共字符串借用选项{get;set;}
}
这是我的上下文类
公共FilipLibraryContext(DbContextOptions)
:基本(选项)
{
}
公共数据库集书籍{get;set;}
公共数据库集库客户{get;set;}
公共数据库集{get;set;}
公共数据库集借用历史跟踪{get;set;}
公共数据库集库CustomerTrackings{get;set;}
}
在表借阅历史跟踪中,我想记录借阅的书籍。它是借阅历史的一种副本
,但也包含过去的预订<代码>借阅历史记录
只包含已预订的内容,不保留对过去事件的跟踪
行动方法是
公共异步任务OnPostAsync(int?id)
{
if(id==null)
{
返回NotFound();
}
BorrowHistory=Wait_context.BorrowHistories.FindAsync(id);
LibraryCustomer=wait_context.LibraryCustomers.FindAsync(借用历史记录.LibraryCustomerId);
BorrowHistoryTracking=Wait_context.BorrowHistoryTrackings.FindAsync(id);
LibraryCustomerTracking=wait_context.LibraryCustomerTrackings.FindAsync(借入历史跟踪.LibraryCustomerTrackingId);
if(借用历史!=null)
{
_上下文。借用历史记录。删除(借用历史记录);
wait_context.SaveChangesAsync();
}
if(LibraryCustomer!=null)
{
_context.LibraryCustomers.Remove(LibraryCustomer);
wait_context.SaveChangesAsync();
}
if(借用历史跟踪!=null)
{
var BorrowHistoryTracking=wait _context.BorrowHistoryTrackings.FindAsync(id);
BorrowHistoryTracking.ReturnDate=DateTime.Today.Date;
_context.Entry(借入历史跟踪).State=EntityState.Modified;
wait_context.SaveChangesAsync();
}
if(LibraryCustomerTracking!=null)
{
wait_context.SaveChangesAsync();
}
返回重定向Topage(“../Books/Index”);
}
此方法删除借阅历史记录
表中的预订,但应保留借阅历史记录跟踪
表中的预订跟踪
第二次更新
namespace Library.Pages.com
{
公共类DeleteModel:PageModel
{
私有只读FilipLibraryContext_context;
公共删除模型(FilipLibraryContext)
{
_上下文=上下文;
}
[BindProperty]
公共借阅历史{get;set;}
[BindProperty]
公共图书馆客户图书馆客户{get;set;}
public BorrowHistoryTracking BorrowHistoryTracking{get;set;}
公共图书馆CustomerTracking图书馆CustomerTracking{get;set;}
公共异步任务OnGetAsync(int?id)
{
if(id==null)
{
返回NotFound();
}
BorrowHistory=等待上下文
.包括(b=>b.Book)
.Include(b=>b.LibraryCustomer).FirstOrDefaultAsync(m=>m.BorrowHistoryId==id);
BorrowHistoryTracking=等待上下文。BorrowHistoryTrackings
.包括(b=>b.Book)
.Include(b=>b.LibraryCustomerTracking).FirstOrDefaultAsync(m=>m.BorrowHistoryTrackingId==id);
如果(借用历史==null)
{
返回NotFound();
}
返回页();
}
公共异步任务OnPostAsync(int?id)
{
if(id==null)
{
返回NotFound();
}
BorrowHistory=Wait_context.BorrowHistories.FindAsync(id);
LibraryCustomer=wait_context.LibraryCustomers.FindAsync(借用历史记录.LibraryCustomerId);
BorrowHistoryTracking=Wait_context.BorrowHistoryTrackings.FindAsync(id);
LibraryCustomerTracking=wait_context.LibraryCustomerTrackings.FindAsync(借入历史跟踪.LibraryCustomerTrackingId);
if(借用历史!=null)
{
_上下文。借用历史记录。删除(借用历史记录);
wait_context.SaveChangesAsync();
}
if(LibraryCustomer!=null)
{
_context.LibraryCustomers.Remove(LibraryCustomer);
wait_context.SaveChangesAsync();
}
if(借用历史跟踪!=null)
{
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
var bookId BorrowHistoryTracking.BookId;
var existingTracking = await
_context.BorrowHistoryTrackings.FirstOrDefaultAsync(i=> i.BookId==bookId);
.... and so on
BorrowHistoryTracking.ReturnDate = DateTime.Today.Date;
_context.Attach(BorrowHistoryTracking).State = EntityState.Modified;