简体   繁体   English

在实体框架中调用 AsNoTracking 的位置是否重要

[英]Does it matter where AsNoTracking in Entity Framework is called

Does it matter where the AsNoTracking method is called when writing an Entity Framework query?编写实体框架查询时,在何处调用 AsNoTracking 方法是否重要? eg例如

var matchingCustomers = context.Customers.AsNoTracking().Where(n => n.city == "Milan").Skip(50).Take(100).OrderBy(n => n.Name).ToList();
var matchingCustomers = context.Customers.Where(n => n.city == "Milan").AsNoTracking().Skip(50).Take(100).OrderBy(n => n.Name).ToList();
var matchingCustomers = context.Customers.Where(n => n.city == "Milan").Skip(50).AsNoTracking().Take(100).OrderBy(n => n.Name).ToList();
var matchingCustomers = context.Customers.Where(n => n.city == "Milan").Skip(50).Take(100).AsNoTracking().OrderBy(n => n.Name).ToList();
var matchingCustomers = context.Customers.Where(n => n.city == "Milan").Skip(50).Take(100).OrderBy(n => n.Name).AsNoTracking().ToList();
var matchingCustomers = context.Customers.Where(n => n.city == "Milan").Skip(50).Take(100).OrderBy(n => n.Name).ToList().AsNoTracking();

I like adding it to the end of statements but before the ToList is called like this:我喜欢将它添加到语句的末尾,但在 ToList 被这样调用之前:

var matchingCustomers = context.Customers.Where(n => n.city == "Milan").Skip(50).Take(100).OrderBy(n => n.Name).AsNoTracking().ToList();

No it doesn't matter: (source)不没关系:( 来源)

A new query with NoTracking applied, or the source query if NoTracking is not supported.应用 NoTracking 的新查询,如果不支持 NoTracking,则使用源查询。

So you either do it in the beginning and you expand the "new" query with the method chain, or you do it in the end and then get the "new" query.因此,您要么在开始时执行此操作,然后使用方法链扩展“新”查询,要么在最后执行此操作,然后获取“新”查询。 As long as you call it before the query is executed you're fine.只要您在执行查询之前调用它就可以了。

I think that我觉得

var matchingCustomers = context.Customers.Where(n => n.city == "Milan").Skip(50).Take(100).OrderBy(n => n.Name).ToList().AsNoTracking();

matters because you are trying to apply NoTracking to a data structure already in memory once EF has executed, and traked, the query.很重要,因为一旦 EF 执行并跟踪了查询,您就会尝试将 NoTracking 应用于内存中已有的数据结构。

When you use this fluent API;当你使用这个流畅的 API 时; you are defining a query without executing it until you, of course, execute the query.您正在定义一个查询而不执行它,当然,直到您执行该查询。 ToList() will execute the query an bring the data to memory to transform it into a List<T> data structure. ToList()将执行查询并将数据带入内存以将其转换为List<T>数据结构。

Let's split the command to understand this:让我们拆分命令来理解这一点:

  • context.Customers --> Select [*] from Customers context.Customers --> 从客户中选择 [*]
  • Where(n => n.city == "Milan") --> Select [*] from Customers where city == 'Milan' Where(n => n.city == "Milan") --> 选择 [*] from Customers where city == 'Milan'
  • Skip(50).Take(100) --> Select [*] from Customers where city == 'Milan' OFFSET 50 ROWS FETCH NEXT 100 ROWS ONLY Skip(50).Take(100) --> 选择 [*] from Customers where city == 'Milan' OFFSET 50 ROWS FETCH NEXT 100 ROWS ONLY
  • OrderBy name --> Select [*] from Customers where city == 'Milan' OFFSET 50 ROWS FETCH NEXT 100 ROWS ONLY ORDER BY name OrderBy name --> Select [*] from Customers where city == 'Milan' OFFSET 50 ROWS FETCH NEXT 100 ROWS ONLY ORDER BY name
  • ToList() --> Execute the query and bring the data into memory with Tracking by default! ToList() --> 执行查询,默认使用Tracking将数据带入内存!
  • AsNoTraking() --> Does nothing because EF already executed the query and tracked the data. AsNoTraking() --> 什么都不做,因为 EF 已经执行了查询并跟踪了数据。

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

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