Click here to Skip to main content
15,397,791 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have changed the edit function to update any field based on the necessity. There are two fields to update. One is file name other one is image file. Updating the image file only or with the name no issue but if I want to update name only then "unhandled exception" occurred.

What I have tried:

C#
[HttpPost]
    [ValidateAntiForgeryToken]
    //public async Task<IActionResult> Edit([Bind("ImgID,Title,ImageFile")] IFormFile fileobj, imageModels imgModels, string fileName, int id)
    public async Task<IActionResult> Edit(imageModels imgModels, int id, IFormFile ImageFile)
    {
        var imageModels = await _context.Images.FindAsync(id);

        if (ImageFile != null)
        {
           
            var imagePath = Path.Combine(_hostEnvironment.WebRootPath, "images", imageModels.ImgName);
            if (System.IO.File.Exists(imagePath))
                System.IO.File.Delete(imagePath);
            _context.Images.Remove(imageModels);
       
            string wwwRootPath = _hostEnvironment.WebRootPath;
            string fileName = Path.GetFileNameWithoutExtension(imgModels.ImageFile.FileName);
            string extension = Path.GetExtension(imgModels.ImageFile.FileName);
            imgModels.ImgName = fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension;
            string path = Path.Combine(wwwRootPath + "/images/", fileName);
            using (var fileStream = new FileStream(path, FileMode.Create))
            {
                //await imgModels.ImageFile.CopyToAsync(fileStream);
                imgModels.ImageFile.CopyTo(fileStream);
            }
 
        }

        _context.Update(imgModels);
        await _context.SaveChangesAsync();
        return RedirectToAction(nameof(Index));
        //return View(imgModels);
    }
Posted
Updated 13-Jun-22 23:44pm
v2
Comments
Graeme_Grant 14-Jun-22 5:46am
   
Which line is the error thrown on?
Faisal_Raj 14-Jun-22 7:12am
   
"_context.Update(imgModels);" - this following line showing the error while trying to update name filed only.
Richard MacCutchan 14-Jun-22 6:09am
   
Also, what is the full text of the error message?
Faisal_Raj 14-Jun-22 7:20am
   
"InvalidOperationException: The instance of entity type 'imageModels' cannot be tracked because another instance with the same key value for {'ImgID'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values." -

Here is the confusion. Updating all fields no issues but updating individually name field having this issue.
If I try to update name filed only without uploading any file the function will not go into if condition but showing this error.
Richard MacCutchan 14-Jun-22 10:53am
   
Sorry, I do not know what that refers to. You need to look at your design, and how it fits the EF rules.
Gerry Schmitz 14-Jun-22 17:12pm
   
Maybe you should do a "save" after your "remove". You're mixing transaction "duties".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900