實體框架上下文未正確加載

[英]Entity Framework context is incorrectly loaded


In the following code snippet I'm placing a brakpoint at the return of the select.

在下面的代碼片段中,我在select的返回時放置了一個brakpoint。

In this case the breakpoint is being hit correctly

在這種情況下,正確地命中斷點

                         context.campaigns
                         .Include("adgroups")
                         .Include("campaigntimeranges")
                         .ToList()
                         .Where(p=> p.LastUpdate < fromDate && 
                                    p.campaigntimeranges.OrderByDescending(q => q.EntryDate).FirstOrDefault().DateStart >= fromDate && 
                                    p.campaigntimeranges.OrderByDescending(q => q.EntryDate).FirstOrDefault().DateStart <= currentDate
                               )
                         .ToList().Select(x => { 
                             return x; //Breakpoint here
                         }).ToList();

In that case the (.ToList() is removed) the breakpointis never hit

在這種情況下(.ToList()被刪除)斷點永遠不會被擊中

                         context.campaigns
                         .Include("adgroups")
                         .Include("campaigntimeranges")
                         .Where(p=> p.LastUpdate < fromDate && 
                                    p.campaigntimeranges.OrderByDescending(q => q.EntryDate).FirstOrDefault().DateStart >= fromDate && 
                                    p.campaigntimeranges.OrderByDescending(q => q.EntryDate).FirstOrDefault().DateStart <= currentDate
                               )
                         .ToList().Select(x => { 
                             return x; //Breakpoint here
                         }).ToList();

I wish I could use the first, working example however anything after the .ToList() is not loaded into context so the first one results in a collection of all "campaigns".

我希望我可以使用第一個工作示例但是.ToList()之后沒有加載到上下文中,所以第一個導致所有“廣告系列”的集合。

Why is that and how can I fix that?

為什么這樣,我該如何解決?

Thanks

謝謝

1 个解决方案

#1


3  

Function lambda inside Select function is not called because (watch for it!) it's NOT executed in your application. Entity framework translates this expression into an SQL expression and then executes it within SQL server.

函數lambda里面沒有調用Select函數,因為(注意它!)它不會在你的應用程序中執行。實體框架將此表達式轉換為SQL表達式,然后在SQL Server中執行它。

When you add call to ToList function, it forces EF to execute query and return a result. ToList as you know returns an List<T> and for that reason Select is executed within your application and you can break there using a breakpoint.

當您添加對ToList函數的調用時,它會強制EF執行查詢並返回結果。如您所知,ToList返回List ,因此Select在您的應用程序中執行,您可以使用斷點在那里中斷。

context.campaigns.Where(/*condition*/).Select(/*selector*/); // returns IQueryable<T>
// IQueryable is translated into a SQL statement

var campaigns = context.campaigns.Where(/*condition*/).ToList(); // Returns List<T>
campaign.Select(/*selector*/); // campaign is a List<T>, and Select is not not translated to SQL, but executed directly

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/04/08/720c949728a6af93d7b5dff18fd82e71.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com