How to update a record without reselecting this record in the ADO.NET Entity Framework?

Hi everyone, I'm doing something like this -

void update(ClasstoUpdate obj)//obj is already having values to update... { var data= (from i in Entityobject.ClasstoUpdate where obj.Id==i.Id select i).FirstorDefault(); data.Name="SomeCoolName"; EntityObject.SaveChanges(); } 

I want to perform an update without re-querying using the identifier, is there a way that I just pass the updated object to the Entity ADO.NET infrastructure and it updates it. I'm sorry if I missed something, but this way, I do this by wondering if there is an easy way to update. Thanks.

+4
source share
1 answer

An easy way to update an object is to select it, modify it, and submit the changes that you already make.

Another way is to attach the object and inform the infrastructure of the entity that the object is in an altered state.

The third way is to update the object by building an SQL string that updates the object directly in the database without getting it. However, I would not recommend doing this.

Note: Remember to check the null value in your function. If you know that the return value of FirstOrDefault will never be zero, then you should use First . You may also consider using Single instead of First .

+6
source

Source: https://habr.com/ru/post/1310161/


All Articles