Update attempt in .NET Core throws exception

asp.net-core c# entity-framework-core

Question

I am developing an application where I do not necessarily need to use .NET core identity for login. However, whenever I try updating a record, it throws the following exception

The instance of entity type 'StApplications' cannot be tracked because another instance with the same key value for {'ApplicationId'} 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.

Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap.ThrowIdentityConflict(InternalEntityEntry entry)

StApplications is the table where I want to update record

Below is the code in my ApplicationRepository

public async  Task<StApplications> UpdateAsync(StApplications obj)
{
    var updatedApplication = _context.Update(obj);
     await _context.SaveChangesAsync();
    return updatedApplication.Entity;
}

Followed by the code in my ApplicationController, the POST method

public async Task<IActionResult> Biodata(StApplications stApplications)
{
    var application = await _ApplicationRepository.GetByIdAsync(stApplications.ApplicationId) ;

    if (application == null)
    {
        return BadRequest(ModelState);
    }

    var updatedapplication = await _ApplicationRepository.UpdateAsync(stApplications);
    return RedirectToAction("Olevel", new { id = updatedapplication.ApplicationId});
}
1
0
9/30/2019 11:52:36 AM

Popular Answer

You should not be saving stApplications, but rather, mapping the values from that onto the application instance you pulled from the database, and then saving application.

0
9/30/2019 12:45:39 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow