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?
– novic
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.
– Hiago Souza
I guess the way EF works doesn’t do that
– novic
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.
– Hiago Souza
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
– novic