LinqToSql static DataContext in a web application

后端 未结 5 1108
囚心锁ツ
囚心锁ツ 2021-01-02 12:14

In a web application that I have run across, I found the following code to deal with the DataContext when dealing with LinqToSQL

public partial class DbDataC         


        
5条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-02 12:48

    I prefer to create a Page base class (inherit from System.Web.UI.Page), and expose a DataContext property. It ensures that there is one instance of DataContext per page request.

    This has worked well for me, it's a good balance IMHO. You can just call DataContext.SubmitChanges() at the end of the page and be assured that everything is updated. You also ensure that all the changes are for one user at a time.

    Doing this via static will cause pain -- I fear DataContext will lose track of changes since it's trying to track changes for many users concurrently. I don't think it was designed for that.

提交回复
热议问题