1
Good night, you guys!
I’m new to ASP NET MVC and I’m making a CRUD
simple with bench Postgresql. I’ve already implemented the Insert
, delete and Get
, but the Update
is not saving the changes in the bank. Debugging, I realized that it does all the steps correctly, until the "commit
" of UPDATE
, but when I give GET
again for that record, it remains the same. No error returns or nothing. Follow my method below:
public Guid Update(AreaInteresse area)
{
using (NpgsqlConnection con = new NpgsqlConnection(connectionString))
{
con.Open();
//Inicia a transação
using (var trans = con.BeginTransaction())
{
try
{
NpgsqlCommand cmd = new NpgsqlCommand();
cmd.Connection = con;
cmd.Transaction = trans;
cmd.CommandText = @"UPDATE areainteresse SET descricao=@descricao WHERE id= @id";
cmd.Parameters.AddWithValue("id", Convert.ToString(area.Id));
cmd.Parameters.AddWithValue("descricao", area.Descricao);
cmd.ExecuteNonQuery();
//commit na transação
trans.Commit();
return area.Id;
}
catch (Exception ex)
{
//rollback da transação
trans.Rollback();
throw ex;
}
finally
{
con.Close();
}
}
}
Does anyone have any idea what it could be? It seems that he is not executing the "COMMIT" in the database, because the ID arrives correctly to the Parameter and converts it to string because the database field is a Character Varying (36).
The Id field should not be automatically updated after the Commit. You would need to refresh this instance. Check the database to see if the data is being updated or not. And another detail is that you should never need to change an entity’s id.
– Phiter
The Id field is not automatically updated. All attributes of the class (including the id of the class to be updated) are passed, but only the attribute "Description" of the Areainteresse table is updated. Consulting the bank by Pgadmin, the data remains unchanged.
– AK_scout