如何强制Linq使用SQL不使用缓存?

时间:2020-12-07 21:14:38

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.

这是一个Windows窗体应用程序,我在应用程序启动时创建dataContext。

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

如何强制Linq SQL不使用缓存?

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; 
}

2 个解决方案

#1


10  

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?

最简单的方法是使用新的DataContext - 假设上下文提供的大部分内容是缓存和身份管理,听起来你只是想要一个新的上下文。为什么你想创建一个然后坚持下去?

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

顺便说一句,对于像你这样的简单查询,使用“普通”C#和扩展方法而不是查询表达式更具可读性(IMO):

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.

编辑:如果您从不希望它跟踪更改,则在执行任何操作之前将ObjectTrackingEnabled设置为false。但是,这将严重限制其有用性。你不能只是向前和向后翻转开关(在它们之间进行查询)。 IMO,改变你的设计以避免单身上下文会好得多。

#2


0  

It can matter HOW you add an object to the DataContext as to whether or not it will be included in future queries.

如何向DataContext添加一个对象,以确定它是否将包含在将来的查询中。

Will NOT add the new InventoryTransaction to future in memory queries

不会将新的InventoryTransaction添加到内存查询中的未来

In this example I'm adding an object with an ID and then adding it to the context.

在这个例子中,我添加了一个带有ID的对象,然后将其添加到上下文中。

var transaction = new InventoryTransaction()
                 {
                     AdjustmentDate = currentTime,
                     QtyAdjustment = 5,
                     InventoryProductId = inventoryProductId
                 };

dbContext.InventoryTransactions.Add(transaction);
dbContext.SubmitChanges();

Linq-to-SQL isn't clever enough to see this as needing to be added to the previously cached list of in memory items in InventoryTransactions.

Linq-to-SQL不够聪明,不能将其视为需要添加到InventoryTransactions中以前缓存的内存项列表中。

WILL add the new InventoryTransaction to future in memory queries

将把新的InventoryTransaction添加到内存查询中的未来

var transaction = new InventoryTransaction()
                 {
                     AdjustmentDate = currentTime,
                     QtyAdjustment = 5
                 };

inventoryProduct.InventoryTransactions.Add(transaction);
dbContext.SubmitChanges();

Wherever possible use the collections in Linq-to-SQL when creating relationships and not the IDs.

在创建关系而不是ID时,尽可能使用Linq-to-SQL中的集合。

In addition as Jon says, try to minimize the scope of a DataContext as much as possible.

此外,正如Jon所说,尽量尽量减少DataContext的范围。

#1


10  

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?

最简单的方法是使用新的DataContext - 假设上下文提供的大部分内容是缓存和身份管理,听起来你只是想要一个新的上下文。为什么你想创建一个然后坚持下去?

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

顺便说一句,对于像你这样的简单查询,使用“普通”C#和扩展方法而不是查询表达式更具可读性(IMO):

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.

编辑:如果您从不希望它跟踪更改,则在执行任何操作之前将ObjectTrackingEnabled设置为false。但是,这将严重限制其有用性。你不能只是向前和向后翻转开关(在它们之间进行查询)。 IMO,改变你的设计以避免单身上下文会好得多。

#2


0  

It can matter HOW you add an object to the DataContext as to whether or not it will be included in future queries.

如何向DataContext添加一个对象,以确定它是否将包含在将来的查询中。

Will NOT add the new InventoryTransaction to future in memory queries

不会将新的InventoryTransaction添加到内存查询中的未来

In this example I'm adding an object with an ID and then adding it to the context.

在这个例子中,我添加了一个带有ID的对象,然后将其添加到上下文中。

var transaction = new InventoryTransaction()
                 {
                     AdjustmentDate = currentTime,
                     QtyAdjustment = 5,
                     InventoryProductId = inventoryProductId
                 };

dbContext.InventoryTransactions.Add(transaction);
dbContext.SubmitChanges();

Linq-to-SQL isn't clever enough to see this as needing to be added to the previously cached list of in memory items in InventoryTransactions.

Linq-to-SQL不够聪明,不能将其视为需要添加到InventoryTransactions中以前缓存的内存项列表中。

WILL add the new InventoryTransaction to future in memory queries

将把新的InventoryTransaction添加到内存查询中的未来

var transaction = new InventoryTransaction()
                 {
                     AdjustmentDate = currentTime,
                     QtyAdjustment = 5
                 };

inventoryProduct.InventoryTransactions.Add(transaction);
dbContext.SubmitChanges();

Wherever possible use the collections in Linq-to-SQL when creating relationships and not the IDs.

在创建关系而不是ID时,尽可能使用Linq-to-SQL中的集合。

In addition as Jon says, try to minimize the scope of a DataContext as much as possible.

此外,正如Jon所说,尽量尽量减少DataContext的范围。