In LINQ-SQL, wrap the DataContext is an using statement - pros cons

前端 未结 5 1484
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-02 10:41

Can someone pitch in their opinion about pros/cons between wrapping the DataContext in an using statement or not in LINQ-SQL in terms of factors as performance, memory usage, ea

5条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-02 11:21

    1. First time DataContext will get object from DB.
    2. Next time you fire a query to get the same object (same parameters).: You’ll see query in a profiler but your object in DataContext will not be replaced with new one from DB !!

    Not to mention that behind every DataContext is identity map of all objects you are asking from DB (you don’t want to keep this around).

    Entire idea of DataContext is Unit Of Work with Optimistic Concurrency. Use it for short transaction (one submit only) and dispose.

    Best way to not forget dispose is using ().

提交回复
热议问题