查询实体框架缓存 [英] Query about Entity Framework caching

查看:130
本文介绍了查询实体框架缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有这三种方法:

  public Customer GetCustomerByCustomerGuid(Guid customerGuid)
{
返回GetCustomers()。FirstOrDefault(c => c.CustomerGuid.Equals(customerGuid));
}

public Customer GetCustomerByEmailAddress(string emailAddress)
{
return GetCustomers()。FirstOrDefault(c => c.EmailAddress.Equals(emailAddress,StringComparison OrdinalIgnoreCase));
}

public IEnumerable< Customer> GetCustomers()
{
从_customerRepository.Table中的r返回选择r;
}

//_customerRepository.Table是这样的:
public IQueryable< T>表
{
get {return Entities; }
}

每当我拨打电话时,都会对数据库发出查询GetCustomerByEmailAddress() / GetCustomerByCustomerGuid()或者EF缓存 GetCustomer()并查询我的信息?



另一方面,它会将每个调用的结果缓存到 GetCustomerByEmailAddress () / GetCustomerByCustomerGuid()



我正在尝试建立手册缓存我应该去,我真的不喜欢运行更多的SQL查询比绝对必要。

解决方案

它将导致一个调用数据库每次。我今天早些时候问过类似的问题,你可以在这里看到更多的内容:为什么Entity Framework 6.x不缓存结果?


Lets say I have these three methods:

public Customer GetCustomerByCustomerGuid(Guid customerGuid)
{
    return GetCustomers().FirstOrDefault(c => c.CustomerGuid.Equals(customerGuid));
}

public Customer GetCustomerByEmailAddress(string emailAddress)
{
    return GetCustomers().FirstOrDefault(c => c.EmailAddress.Equals(emailAddress, StringComparison.OrdinalIgnoreCase));
}

public IEnumerable<Customer> GetCustomers()
{
    return from r in _customerRepository.Table select r;
}

//_customerRepository.Table is this:
public IQueryable<T> Table
{
    get { return Entities; }
}

Would this cause a query to the database each time I make a call to GetCustomerByEmailAddress() / GetCustomerByCustomerGuid() or would EF cache the results of GetCustomer() and query that for my information?

On the other hand, would it just cache the result of each call to GetCustomerByEmailAddress() / GetCustomerByCustomerGuid()

I am trying to establish the level of manual caching I should go to, I really dislike running more SQL queries than are absolutely necessary.

解决方案

It will result in a call to the database every time. I actually asked a similar question earlier today and you can see more here: Why does Entity Framework 6.x not cache results?

这篇关于查询实体框架缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆