[英]Unable to update a record in asp.net-core 2.2 c#
我正在尝试更新项目的记录,如下所示
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,Label,Description,LessonId,Position,FileName,Format,Status")] Content content)
{
if (id != content.Id)
{
return NotFound();
}
var existingContent = await _context.Contents.FindAsync(id).ConfigureAwait(false);
if (existingContent == null)
{
return NotFound();
}
string fileToDelete = null;
if (ModelState.IsValid)
{
try
{
if (Request.Form.Files["FileName"] != null)
{
IFormFile uploadedFile = Request.Form.Files["FileName"];
var lesson_mimetype = uploadedFile.ContentType;
string[] extensions = new string[] { ".jpeg", ".jpg", ".gif", ".png", ".mp4", ".mp3", ".pdf" };
ResponseMsg fileTypeValidationMsg = FileUploadHelper.ValidateFileExtension(uploadedFile, extensions);
if (!fileTypeValidationMsg.ResponseStatus)
{
ModelState.AddModelError("FileName", fileTypeValidationMsg.ResponseDescription);
}
ResponseMsg filesizeValidationMsg = FileUploadHelper.ValidateFilesize(uploadedFile, 200);
if (!filesizeValidationMsg.ResponseStatus)
{
ModelState.AddModelError("FileName", filesizeValidationMsg.ResponseDescription);
}
if (content.Format == ResourceFormat.Text)
{
string desiredUploadDirectory = "TextResources";
string lesson_file_name = await uploadHelper.SaveFileAsync(Request.Form.Files["FileName"], desiredUploadDirectory).ConfigureAwait(false);
existingContent.TextFileUrl = desiredUploadDirectory + "/" + lesson_file_name;
existingContent.FileName = lesson_file_name;
fileToDelete = existingContent.TextFileUrl;
}
else
{
string desiredUploadDirectory = "MultimediaResources";
string lesson_file_name = await uploadHelper.SaveFileAsync(Request.Form.Files["FileName"], desiredUploadDirectory).ConfigureAwait(false);
existingContent.MultiMediaFileUrl = desiredUploadDirectory + "/" + lesson_file_name;
existingContent.FileName = lesson_file_name;
fileToDelete = existingContent.MultiMediaFileUrl;
}
}
existingContent.LastUpdated = DateTime.Now;
existingContent.Label = content.Label;
existingContent.Description = content.Description;
existingContent.LessonId = content.LessonId;
existingContent.Position = content.Position;
existingContent.Status = content.Status;
existingContent.Format = content.Format;
//_context.Update(existingContent); Now removed for the code to work
await _context.SaveChangesAsync().ConfigureAwait(false);
if (fileToDelete != null)
{
System.IO.File.Delete(fileToDelete);
}
return RedirectToAction(nameof(Index));
}
catch (Exception e)
{
ModelState.AddModelError("", e.Message);
}
}
ViewData["LessonId"] = new SelectList(_context.Lessons, "Id", "Label", content.LessonId);
return View(content);
}
但是问题是,当我提交更新表单时,出现以下错误消息
无法跟踪实体类型“内容”的实例,因为已经跟踪了具有相同的{'Id'}键值的另一个实例。 附加现有实体时,请确保仅附加一个具有给定键值的实体实例。 考虑使用'DbContextOptionsBuilder.EnableSensitiveDataLogging'来查看冲突的键值。
我不知道该如何解决。 我将不胜感激任何指导。
在大多数情况下,EF会自动检测哪些字段需要更新,如果在上下文中(带有实体)具有update
功能,这是一个危险信号,表示您正在尝试执行不需要的操作。
EF在幕后做了很多事情,因此您不必考虑它们。 这就是其中之一。
我怀疑您读了一个非常古老的博客,它讲述了10年前某人必须如何做才能使EF更新记录。
另外,从您的上下文类中删除该update
函数,以便没有其他人跳上它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.