Singleton pattern for data context

只愿长相守 提交于 2019-12-13 05:45:43

问题


I am using singleton pattern in an ASP.net application. I use LINQ to SQL DataContext.

I face this problem: when a new record is inserted using the DataContext, it is inserted into the database when the changes are submitted. But when the same data is queried using the DataContext, the data is not retrieved, but it's read when closing and opening the application again.

Please let me know how to solve this issue.

I user C# 4.0, LINQ, SQL Server 2008.

Regards, Jebli


回答1:


An instance of DataContext is not meant to be used as a Singleton. An instance of DataContext is meant to be used in a UnitOfWork (a completely different pattern).

If you use the appropriate lifecycle pattern for instances of DataContext, you will rarely have to deal with stale data and never have to deal with a single database connection shared between multiple threads.



来源:https://stackoverflow.com/questions/4735498/singleton-pattern-for-data-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!