开发者

how to update part of object

开发者 https://www.devze.com 2023-01-27 11:31 出处:网络
It is necessary to insert some data in DB once each web service method is called: in the beginning of the request processing and in the end.

It is necessary to insert some data in DB once each web service method is called: in the beginning of the request processing and in the end.

My intention is to insert record that will contains all income information in the beginning of request proces开发者_JAVA技巧sing and after that update the same record once request is processed and data are ready to be send back (or error is occurred and I is need to store error message).

The problem is that income data can be pretty long and LINQ To SQL before update need to fetch object data from DB and then "store" it again. In this case "income data" is going 3 times:

  1. 1st time when inserting - it goes into DB;
  2. 2nd time before object update - it is fetched from DB;
  3. 3rd time on update - it is going to DB again.

Is there any possibility to optimize such process if I already have object fetched from DB?

Is the same applied to Entity Framework? Does it allow to update only the part of object?


An ORM is geared towards converting complete rows to complete objects, and back again - so updates are always to the full object.

However, both Linq-to-SQL as well as Entity Framework are definitely smart enough to find out what properties have changed on an entity, so if you only update some fields, the generated SQL command using UPDATE will only update those changed fields.

So basically: you just try it! Fire up SQL profiler and see what SQL goes to the database; in Entity Framework, I'm positive that if you only change some fields, only those changed fields will be updated in an UPDATE statement and nothing else.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号