How to update a record without selecting that record again in ADO.NET Entity Framework?

前端 未结 1 1762
萌比男神i
萌比男神i 2020-12-11 07:33

Hi all I am doing something like this -

void update(ClasstoUpdate obj)//obj is already having values to update...
{
  var data= (from i in Entityobject.Clas         


        
相关标签:
1条回答
  • 2020-12-11 08:34

    The simple way to update an object is fetch it, change it, and submit changes which is what you're already doing.

    Another way is to attach the object, and tell the entity framework that the object is in a modified state.

    A third way is to update the object by constructing an SQL string that updates the object directly in the database without fetching it. However I wouldn't recommend doing this.

    A side note: remember to check for null in your function. If you know the return value of FirstOrDefault will never be null then you should use First instead. You might also want to consider using Single instead of First.

    0 讨论(0)
提交回复
热议问题