简体   繁体   English

DbContext.Find()和DbContext.SingleOrDefault()实体框架核心之间的区别

[英]Different between DbContext.Find() and DbContext.SingleOrDefault() Entity Framework Core

What is the different when query data from context using Find() and Single() . 使用Find()Single()从上下文中查询数据有何不同? Both return the entity being requested. 两者都返回被请求的实体。

Some example I found on Microsoft are using Single , SingleOrDefault variation to query for entity. 我在Microsoft上发现的一些示例使用SingleSingleOrDefault变体查询实体。 Some uses Find method. 有些使用Find方法。

Are there any "performance" advantanges when using one over the other? 当一个使用另一个时,是否有任何“性能”优势?

Although they look the same they are very different in some fundamental ways 尽管它们看起来相同,但在某些基本方面却有很大不同

In short, Find starts by searching in the local cache of the context. 简而言之, Find始于在上下文的本地缓存中搜索。 If no match are found then it sends a query to the db. 如果找不到匹配项,则它将查询发送到数据库。

Documentation is your friend 文档是您的朋友

DbSet.Find Method DbSet.Find方法

Finds an entity with the given primary key values. 查找具有给定主键值的实体。 If an entity with the given primary key values exists in the context, then it is returned immediately without making a request to the store. 如果上下文中存在具有给定主键值的实体,则将立即返回该实体,而无需向商店提出请求。 Otherwise, a request is made to the store for an entity with the given primary key values and this entity, if found, is attached to the context and returned. 否则,将向商店请求具有给定主键值的实体,如果找到该实体,则将该实体附加到上下文并返回。 If no entity is found in the context or the store, then null is returned. 如果在上下文或存储中未找到任何实体,则返回null。

Queryable.SingleOrDefault Method Queryable.SingleOrDefault方法

Returns a single, specific element of a sequence, or a default value if no such element is found. 返回序列中的单个特定元素,如果找不到此类元素,则返回默认值。

Queryable.FirstOrDefault Method Queryable.FirstOrDefault方法

Returns the first element of a sequence, or a default value if no element is found. 返回序列的第一个元素,如果找不到元素,则返回默认值。

More-so 更使

Querying and Finding Entities 查询和查找实体

The Find method on DbSet uses the primary key value to attempt to find an entity tracked by the context. DbSet上的Find方法使用主键值尝试查找上下文跟踪的实体。 If the entity is not found in the context then a query will be sent to the database to find the entity there. 如果在上下文中未找到该实体,则将向数据库发送查询以在该数据库中找到该实体。 Null is returned if the entity is not found in the context or in the database. 如果在上下文或数据库中找不到该实体,则返回Null。

Find is different from using a query in two significant ways: 查找与在两种主要方式中使用查询不同:

  • A round-trip to the database will only be made if the entity with the given key is not found in the context. 仅当在上下文中未找到具有给定密钥的实体时,才进行数据库往返。
  • Find will return entities that are in the Added state. 查找将返回处于“已添加”状态的实体。 That is, Find will return entities that have been added to the context but have not yet been saved to the database. 即,“查找”将返回已添加到上下文但尚未保存到数据库的实体。

Update 更新

Does this mean that if the entitiy was already being tracked (through lazy loading), then Find would actually have a better performance advantange when trying to querying again? 这是否意味着如果已经(通过延迟加载)对实体进行了跟踪,那么在尝试再次查询时,Find实际上会具有更好的性能优势?

Yes it will have better performance 是的,它将有更好的表现

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

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