How to refresh relational property of a LINQ class?
I have two instances of a program that manipulate same Northwind database.
When I add some records to the database from one of开发者_如何学C the instances (for example adding some orders to Orders table with a customer foreign keyJohn
), I can query these new records from the other instance of the program properly. The problem begins when I want to access these new records using John.Orders
. In this situation, the second instance of the program does not see newly added records. What should I do?The problem you are having is probably related to the time you keep the LINQ to SQL DataContext
class alive. It should typically be destroyed after each unit of work you do with it (since it follows the 'unit of work' design pattern), which typically means after each use case / business transaction.
You are probably keeping the DataContext
class alive during the entire lifetime of the application. The DataContext
class is not suited for this, because it will cache all objects it had once retrieved meaning that your data will get stale.
Create a new DataContext
class for every operation or every time the user opens a new form / screen.
精彩评论