C# EF两次命中数据库。。我做错了什么?
我有以下代码:C# EF两次命中数据库。。我做错了什么?,c#,entity-framework,C#,Entity Framework,我有以下代码: private static List<ProxyServer> proxyServers = new List<ProxyServer>(); private static IEnumerable<Product> products = new List<Product>(); private static CharonTestEntities3 context = new CharonTestEnti
private static List<ProxyServer> proxyServers = new List<ProxyServer>();
private static IEnumerable<Product> products = new List<Product>();
private static CharonTestEntities3 context = new CharonTestEntities3();
public Scrape()
{
proxyServers = context.ProxyServers.ToList();
products = context.Products;
}
private ProxyServer getProxy(int countryID)
{
//TODO: random proxy based on last usage (< 40 seconds)
return proxyServers.FirstOrDefault();
}
private bool getProducts(string asin)
{
var product = products.First(x => x.Asin == asin);
if (products.First().ProductID > 0)
{
return true;
}
return false;
}
public void DoScrape(SearchCriteria criteria)
{
getProducts("A1234543345");
}
private static List proxyServers=new List();
私有静态IEnumerable产品=新列表();
私有静态charontestenties3上下文=新的charontestenties3();
公共刮擦
{
proxyServers=context.proxyServers.ToList();
产品=上下文。产品;
}
专用ProxyServer getProxy(int countryID)
{
//TODO:基于上次使用情况的随机代理(<40秒)
返回proxyServers.FirstOrDefault();
}
私人bool getProducts(字符串asin)
{
var product=products.First(x=>x.Asin==Asin);
if(products.First().ProductID>0)
{
返回true;
}
返回false;
}
公共作废文件(搜索标准)
{
getProducts(“A123454345”);
}
当我运行getProducts()时;它点击数据库查找产品,但我认为它会使用我的产品列表。。有人能解释为什么我的初始scrape=new scrape()没有将产品列表缓存到产品对象吗?因为代码
products=context.products代码>不是列表,而是可查询对象。如果将代码更改为products=context.products.ToList()代码>它将一次查询所有数据的SQL
而且products.First(x=>x.Asin==Asin)
与products.First()完全不同,因此您没有缓存,但可查询对象它会两次命中数据库,因为codeproducts=context.products代码>不是列表,而是可查询对象。如果将代码更改为products=context.products.ToList()代码>它将一次查询所有数据的SQL
而且products.First(x=>x.Asin==Asin)
与products.First()完全不同,因此您没有缓存,但可查询对象它会两次命中数据库,因为codeproducts=context.products代码>不是列表,而是可查询对象。如果将代码更改为products=context.products.ToList()代码>它将一次查询所有数据的SQL
而且products.First(x=>x.Asin==Asin)
与products.First()完全不同,因此您没有缓存,但可查询对象它会两次命中数据库,因为codeproducts=context.products代码>不是列表,而是可查询对象。如果将代码更改为products=context.products.ToList()代码>它将一次查询所有数据的SQL
此外,products.First(x=>x.Asin==Asin)
与products.First()完全不同,因此您没有缓存,但可查询对象它会两次访问数据库您的产品列表类型为IEnumerable()
它在getProducts
上点击数据库,因为这是它真正需要数据的唯一时间
要解决此问题,请在Scrape()
中,将.ToList()
添加到上下文中。产品您的产品列表类型为IEnumerable()
它在getProducts
上点击数据库,因为这是它真正需要数据的唯一时间
要解决此问题,请在Scrape()
中,将.ToList()
添加到上下文中。产品您的产品列表类型为IEnumerable()
它在getProducts
上点击数据库,因为这是它真正需要数据的唯一时间
要解决此问题,请在Scrape()
中,将.ToList()
添加到上下文中。产品您的产品列表类型为IEnumerable()
它在getProducts
上点击数据库,因为这是它真正需要数据的唯一时间
要解决此问题,请在Scrape()
中,将.ToList()
添加到context.Products
中,因为上下文实例上的属性产品正在实现IEnumerable,因此当您将其与字段产品关联时,您只是将可查询对象强制转换为IEnumerable
如果您直接使用context.Products,您将继续看到IEnumerable提供的第一个扩展方法,也可能是任何其他扩展方法(请尝试)
因此,您需要将其强制转换为List,以强制它在ctor上执行查询(与您对ProxyServer所做的相同)
更多:
如果您正在执行第一个没有默认值的操作,它可能会引发异常
我不确定您为什么要验证products上的第一个产品是否具有ProductID>0李>
问题是,上下文实例上的属性产品正在实现IEnumerable,因此,当您将其与字段产品关联时,您只是将可查询对象强制转换为IEnumerable
如果您直接使用context.Products,您将继续看到IEnumerable提供的第一个扩展方法,也可能是任何其他扩展方法(请尝试)
因此,您需要将其强制转换为List,以强制它在ctor上执行查询(与您对ProxyServer所做的相同)
更多:
如果您正在执行第一个没有默认值的操作,它可能会引发异常
我不确定您为什么要验证products上的第一个产品是否具有ProductID>0李>
问题是,上下文实例上的属性产品正在实现IEnumerable,因此,当您将其与字段产品关联时,您只是将可查询对象强制转换为IEnumerable
如果您直接使用context.Products,您将继续看到IEnumerable提供的第一个扩展方法,也可能是任何其他扩展方法(请尝试)
因此,您需要将其强制转换为List,以强制它在ctor上执行查询(与您对ProxyServer所做的相同)
更多:
如果您正在执行第一个没有默认值的操作,它可能会引发异常
我不确定您为什么要验证products上的第一个产品是否具有ProductID>