Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I force Linq to SQL NOT to use the cache?

Tags:

linq-to-sql

When I make the same query twice, the second time it does not return new rows form the database (I guess it just uses the cache).

This is a Windows Form application, where I create the dataContext when the application starts.

How can I force Linq to SQL not to use the cache?

Here is a sample function where I have the problem:

public IEnumerable<Orders> NewOrders()
{
    return from order in dataContext.Orders
           where order.Status == 1
           select order; 
}
like image 516
Thomas Jespersen Avatar asked Nov 18 '08 22:11

Thomas Jespersen


People also ask

Is LINQ to SQL obsolete?

No it is not.

Which is faster LINQ or SQL?

More importantly: when it comes to querying databases, LINQ is in most cases a significantly more productive querying language than SQL. Compared to SQL, LINQ is simpler, tidier, and higher-level.

Is LINQ converted to SQL?

LINQ to SQL offers an infrastructure (run-time) for the management of relational data as objects. It is a component of version 3.5 of the . NET Framework and ably does the translation of language-integrated queries of the object model into SQL. These queries are then sent to the database for the purpose of execution.


1 Answers

The simplest way would be to use a new DataContext - given that most of what the context gives you is caching and identity management, it really sounds like you just want a new context. Why did you want to create just the one and then hold onto it?

By the way, for simple queries like yours it's more readable (IMO) to use "normal" C# with extension methods rather than query expressions:

public IEnumerable<Orders> NewOrders()
{
    return dataContext.Orders.Where(order => order.Status == 1);
}

EDIT: If you never want it to track changes, then set ObjectTrackingEnabled to false before you do anything. However, this will severely limit it's usefulness. You can't just flip the switch back and forward (having made queries between). Changing your design to avoid the singleton context would be much better, IMO.

like image 198
Jon Skeet Avatar answered Nov 03 '22 00:11

Jon Skeet