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
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…