How to instantiate a part of a class with EntityFramework to work with CRUD only on that data

1

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.

    
asked by anonymous 28.11.2017 / 12:17

1 answer

1

LeandroLuk, I recommend that you use some practices to improve the integrity of your code:

1 - Set bank column to 'not null';

2 - Use annotation '[Required]' in the attributes of your class; Example:

internal class Pessoa 
{
    [Required]
    public string primeiroNome { get; set; }
    [Required]
    public string segundoNome { get; set; }
    public int idade { get; set; }
    [Required]
    public string cpf { get; set; }
    // ... mais outros inúmeros campos
}

3 - In your controller, when you receive the object of type 'Person', validate if its state is valid as follows:

internal class PessoaController 
{
    private DbContext db;

    [HttpPut("/")]
    internal IActionResult Update([FromBody] Pessoa pessoa)
    {
       if (ModelState.IsValid)
       {
          //aqui você escreve o código para atualizar
          //Exemplo: 
          db.Pessoas.Update(pessoa);
          db.SaveChanges();

          return Ok();
       }
       else
       {
          return BadRequest(ex.Message);
       }
    }

}
    
04.12.2017 / 21:01