问题
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