views:

33

answers:

1

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.ClasstoUpdate 
            where obj.Id==i.Id
            select i).FirstorDefault();
  data.Name="SomeCoolName";
  EntityObject.SaveChanges();
}

I want to perform an update without again querying using the Id,is there any way I just pass the updated object to ADO.NET Entity framework and it updates it.I am sorry if I am missing something here but this is the way i have been doing it wondering if there is a simple way to update. Thanks.

+1  A: 

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.

Mark Byers
I am sorry i just made an example..ya i would do that...i am going to update the query...what i wanted to know was whether i could just pass that updated object somehow to update
Misnomer