I'm creating a web-based solution in the SinglePageAplication (SPA) type made with AspNetCore and I use the Entity Framework to make my CRUD in the SQL Server database. I am sure that the Entity Framework allows me to do the updates of only what to change in a class so I intend to structure my frontend so that it only sends the attributes to the server that have changed, for example:
Person.cs (the class with all attributes)
internal class Pessoa
{
public string primeiroNome { get; set; }
public string segundoNome { get; set; }
public int idade { get; set; }
public string cpf { get; set; }
// ... mais outros inúmeros campos
}
This class is used in my controller
to receive CRUD from the frontend being:
GET = SELECT | POST = INSERT | PUT = UPDATE | DELETE = DELETE
PersonControler.cs (part of the control that receives ajax as an example)
internal class PessoaController
{
[HttpPut("/")]
internal IActionResult Update([FromBody] Pessoa pessoa)
{
// ... todo o tratamento que preciso para salvar no EF
}
}
Ajax request that is made from my frontend to the update (thinking of a simple call using jQuery as an example)
(function(){
// exemplo de atualização só do nome da pessoa
var pessoa = { nome: 'teste 123' };
$.ajax({
method: 'put',
url: '/',
data: JSON.stringify(pessoa)
success: function(res){ console.log('success', res); },
error: function(res){ console.log('error', res); }
})
})();
My question is: how can I "configure" the EntityFramework so that it always receives a class and if the attribute is null it does not update it in the database? Is there any way to set this up?
Note: The intention is to reduce the burden of data traffic between the frontend and the backend, so I could save resources on both sides.