简体   繁体   English

EF两次击中数据库..我在做什么错?

[英]EF hitting the database twice.. what am i doing wrong?

I have the following code: 我有以下代码:

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

When i run getProducts(); 当我运行getProducts(); it hits the database to look up the product but I thought it would use my products list<>.. can anyone expain why my initial scrape = new Scrape() doesn't cache the products list to the products object? 它命中数据库以查找产品,但是我认为它将使用我的产品列表<> ..谁能解释为什么我最初的scrape = new Scrape()不会将产品列表缓存到产品对象?

Because code products = context.Products; 因为代码products = context.Products; is not a list but querable object. 不是列表,而是可查询对象。 If you change your code to products = context.Products.ToList(); 如果将代码更改为products = context.Products.ToList(); it will query SQL for all data once. 它将一次查询SQL所有数据。

Moreover products.First(x => x.Asin == asin) is something completely different to products.First() and so you don't have a cache, but querable object it hits database twice 而且products.First(x => x.Asin == asin)products.First(x => x.Asin == asin) products.First()完全不同,因此您没有缓存,但是可查询的对象使数据库两次命中

Your products list is of type IEnumerable() . 您的产品列表的类型为IEnumerable()

It hits the DB on getProducts because that is the only time it really needs the data. 它到达了getProducts上的数据库,因为这是它真正需要数据的唯一时间。

To resolve this, in Scrape() , add .ToList() to context.Products 为了解决这个问题,在Scrape()添加.ToList()context.Products

The thing is that the property Products on the context instance is implementing IEnumerable and therefore when you are associating it to your field products you are only casting the querable object to IEnumerable. 事实是, 上下文实例上的属性Products正在实现IEnumerable,因此,当将其与现场产品关联时,您仅将可查询对象转换为IEnumerable。

You will keep seeing the First and probably any other extension method that IEnumerable gives you if you use context.Products directly (please try). 如果直接使用context.Products(请尝试),您将继续看到IEnumerable为您提供的First以及任何其他扩展方法。

So, you need to cast it to List to force it to perform the query on the ctor (the same as you are doing for ProxyServers ). 因此,您需要将其强制转换为List以强制其在ctor上执行查询(与对ProxyServers所做的相同)。

More: 更多:

  1. You are doing a First without Default, it might throw an exception. 您正在执行没有默认值的First,这可能会引发异常。
  2. I'm not sure why you are validating if the first product on products has ProductID > 0. 我不确定为什么要验证产品中的第一个产品的ProductID> 0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM