I have the Users Model follows:
Model Users.cs
[Key]
[Column("cod")]
public int Cod { get; set; }
[Column("nome")]
[StringLength(120)]
public string Nome { get; set; }
[Column("sobrenome")]
[StringLength(80)]
public string Sobrenome { get; set; }
[Column("email")]
[StringLength(60)]
public string Email { get; set; }
[Column("password")]
[StringLength(20)]
public string Password { get; set; }
And I also have the ViewModel only with the data shown in View UsersViewModel.cs
public int Cod { get; set; }
public string Nome { get; set; }
public string Sobrenome { get; set; }
My generic repository looks like this:
public void Update(TEntity obj)
{
Db.Entry(obj).State = EntityState.Modified;
Db.SaveChanges();
}
In my controller it looks like this:
[HttpPost]
public IActionResult SalvaPerfil([FromBody] UsersViewModel usersViewModel)
{
if (ModelState.IsValid)
{
var user = Mapper.Map<UsersViewModel, Users>(usersViewModel);
_PetApp.Update(user);
return Content("fim...");
}
return Content("ERRO");
}
The problem is that the Email and Password fields are being written as NULL in the database, I do not want to change the Email and Password values as they are changed in another View.
I saw this other post, but it is not with Generic Repository, I could not implement it ..
How to update only one field using Entity Framework?
In this case do I need to create a custom method for this change? or is there any way to only change the fields that are in the ViewModel?
Thanks!