Updating only uploaded properties - Entity Framework

Asked

Viewed 166 times

0

I am trying to abstract the methods to save/update the data of my models. I did a generic routine to abstract this operation but I have a little problem. In the scenario below I have the model Pessoa and in my web-api controller I receive it, only instead of sending all class properties I sent a JSON with only the properties that will be updated. The problem is that the Entity tries to update everything including the submitted properties.

Person

class Pessoa {
   public int Id { get; set; }
   public String Nome { get; set; }
   public String Email { get; set; }
}

Assuming I have in table person a record with id "31" and I want to send the json below, the Entity would give an error saying that email cannot be null but I just want to update just the name and wanted it automatically without having to implement class to class. Has anyone gone through this? Thank you.

Example from JSON

{
    "id": 34,
    "nome": "Hiago"
}
  • That’s in every project?

  • Yes @Virgilionovic I am actually creating a standard architecture to implement in my projects. There I am abstracting these things with beforeUpdate methods, afterUpdate these things, to avoid code repetition.

  • I guess the way EF works doesn’t do that

  • So Virgilio, I did it.. But I didn’t like the solution as I let you know if the property was changed or not using dbEntityEntry.Property(Property.Name). Ismodified but to know if the property was changed I check the null or then the 0 in the case of INT and this is unreliable as I may actually want to send null in the service or else 0 in some property.

  • Look maybe use reflection but I don’t know if it’s a good one. You have to see if it’s worth it. Or some package that does it if you think something put

1 answer

1

What do you have to do in this case, in the API you get the ID of the object you want to update right? Then you would have to do the following steps, I will create a scenario here.

This is your model (example):

class Pessoa{
    public int Id { get; set; }
    public string Nome { get; set; }
    public string Email { get; set; }
}

Ai in API you receive (JSON) to update name only:

{
     "Id" : 1,
     "Nome" : "Atualização"
}

you will have to do the following action, search in the database the object referring to that ID:

var pessoa = contexto.Pessoa.First(x => x.Id == model.Id);

update only the field you want in this request (this ensures that if the Model name property that came via JSON is null, keep the previous value):

pessoa.Nome = model.Nome ?? pessoa.Nome;

is after and only save in the database:

contexto.SalveChanges();

If you put null values on the object that will be saved EF-Core cannot identify that it should not (try) write this record in the database, the validation if the field is null or not is a "business rule" and has to be validated before the SaveChanges() what EF-Core does and places NOT NULL prevented recording, if this is configured in database creation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.