I have made a C# Sql API. So far the Read
, create
and delete
work, that means the connection is good, but somehow it is not saving the information in the database. In the code I made some writelines
so you can see some output. I would really appreciate if you would take a look.
CMD (output):
Postman request:
KlantController:
namespace Controllers
{
[ApiController]
[Route("api/Klant")]
[Produces("application/json")]
public class KlantController : ControllerBase
{
private readonly IRepository<KlantModel> _repo;
private readonly DataContext _context;
private readonly IMapper _mapper;
public KlantController(IRepository<KlantModel> repo, IMapper mapper, DataContext context )
{
_repo = repo;
_mapper = mapper;
_context = context;
}
[HttpPut("{id}")]
public async Task<ActionResult<KlantModel>> UpdateService( int id, [FromBody]KlantModel klant)
{
Console.WriteLine("Update Service Method Invoked");
Console.WriteLine("KlantId: " + klant.KlantId + " Klant-Bedrijfsnaam: " + klant.Bedrijfsnaam);
Console.WriteLine("Id: " + id);
if (id != klant.KlantId)
{
return BadRequest("klant object is null");
}
if (!ModelState.IsValid)
{
return BadRequest("Invalid model object");
}
var uptklant = await _repo.GetDataById(id);
Console.WriteLine("ToUpdate-Id: " + uptklant.KlantId + " ToUpdate-Bedrijfsnaam: " + uptklant.Bedrijfsnaam );
if (uptklant is null)
{
return NotFound();
}
_mapper.Map(klant, uptklant);
await _repo.UpdateData(uptklant);
return Ok(klant);
}
}
}
KlantRepository
namespace fixit.Data
{
public class KlantRepository: IRepository<KlantModel>
{
private readonly DataContext _context;
public KlantRepository(DataContext context)
{
_context = context;
}
public async Task<KlantModel> UpdateData(KlantModel klant)
{
_context.Update(klant).Property(x => x.KlantId).IsModified = false;
Console.WriteLine("ToUpdate-Id-Repository: " + klant.KlantId + " ToUpdate-Bedrijfsnaam-Repository: " + klant.Bedrijfsnaam );
await _context.SaveChangesAsync();
return klant;
}
}
}
KlantModel
namespace fixit.Models
{
public class KlantModel
{
[Key]
public int KlantId { get; set; }
[Required]
public string Mailaddres { get; set; }
[Required]
public string Wachtwoord { get; set; }
[Required]
public string Klantvoornaam { get; set; }
[Required]
public string Tussenvoegsel { get; set; }
[Required]
public string Klantachternaam { get; set; }
[Required]
public string Bedrijfsnaam { get; set; }
[Required]
public string Telefoonnummer { get; set; }
}
}
----------------- ! update ! -----------------
It appeared the update did work but I didn't update the value because I didn't gave the new value.
updated the code in KlantController:
From this: await _repo.UpdateData(uptklant);
To this: await _repo.UpdateData(klant);
But now I get a new error:
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware1 An unhandled exception has occurred while executing the request.
System.InvalidOperationException: The instance of entity type 'KlantModel' cannot be tracked because another instance with the same key value for {'KlantId'} 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.
The new error gets stuck at this line of code From KlantRepository:
_context.Update(klant).Property(x => x.KlantId).IsModified = false;
The Update worked good. What i forgot was to change the value that needed to be updated.
This is what i added to KlantController:
uptklant.Mailaddres = klant.Mailaddres;
uptklant.Wachtwoord = klant.Wachtwoord;
uptklant.Klantvoornaam = klant.Klantvoornaam;
uptklant.Tussenvoegsel = klant.Tussenvoegsel;
uptklant.Klantachternaam = klant.Klantachternaam;
uptklant.Bedrijfsnaam = klant.Bedrijfsnaam;
uptklant.Telefoonnummer =klant.Telefoonnummer;
Full code:
namespace Controllers
{
[ApiController]
[Route("api/Klant")]
[Produces("application/json")]
public class KlantController : ControllerBase
{
private readonly IRepository<KlantModel> _repo;
private readonly DataContext _context;
private readonly IMapper _mapper;
public KlantController(IRepository<KlantModel> repo, IMapper mapper, DataContext context )
{
_repo = repo;
_mapper = mapper;
_context = context;
}
[HttpPut("{id}")]
public async Task<ActionResult<KlantModel>> UpdateService( int id, [FromBody]KlantModel klant)
{
Console.WriteLine("Update Service Method Invoked");
Console.WriteLine("KlantId: " + klant.KlantId + " Klant-Bedrijfsnaam: " + klant.Bedrijfsnaam);
Console.WriteLine("Id: " + id);
if (id != klant.KlantId)
{
return BadRequest("klant object is null");
}
if (!ModelState.IsValid)
{
return BadRequest("Invalid model object");
}
var uptklant = await _repo.GetDataById(id);
Console.WriteLine("ToUpdate-Id: " + uptklant.KlantId + " ToUpdate-Bedrijfsnaam: " + uptklant.Bedrijfsnaam );
if (uptklant is null)
{
return NotFound();
}
_mapper.Map(klant, uptklant);
uptklant.Mailaddres = klant.Mailaddres;
uptklant.Wachtwoord = klant.Wachtwoord;
uptklant.Klantvoornaam = klant.Klantvoornaam;
uptklant.Tussenvoegsel = klant.Tussenvoegsel;
uptklant.Klantachternaam = klant.Klantachternaam;
uptklant.Bedrijfsnaam = klant.Bedrijfsnaam;
uptklant.Telefoonnummer =klant.Telefoonnummer;
await _repo.UpdateData(uptklant);
return Ok(klant);
}
}
}