如何在DbSet.Find()中包含相关表? [英] How to include related tables in DbSet.Find()?

查看:168
本文介绍了如何在DbSet.Find()中包含相关表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想在EF7查询中包括相关对象,那就好又容易:

  var myThing = db.MyThings 
.Include(t => t.RelatedThing)
.Where(t => t.SomeCondition == true)
.ToList();

此外,在 DbSet< T> 可以很容易地通过键来加载单个对象:

  var myThing = db.MyThings.Find( ThingId); 

但是现在我要加载 myThing 其ID,以及其 RelatedThing 。不幸的是(并且可以理解) .Find() DbSet< T> 的一种方法,而不是 IQueryable< T> 。显然我可以这样做:

  var myThing = db.MyThings 
.Include(t => t.RelatedThing )
.SingleOrDefault(t => t.MyThingId == somethingId);

但是我特别想使用 .Find()方法,因为它很好且通用,我正在写一种方法,该方法通常将记录与 Expression< Func< T,object>>



任何建议如何执行此操作?

解决方案

使用EF6是不可能的,我认为EF Core不会改变这一点。这是因为 Find 方法的主要目的是从本地缓存中获取已加载的实体,如果不存在,则从数据库中加载它。因此,急切的加载( Include )只能在后一种情况下使用,而在前一种情况下,则需要执行显式加载。

我认为您应该采用 FirstOrDefault (或 SingleOrDefault )路由加上急切的加载。您可以在使用Repository通用方法GetById的EF6中实现示例渴望加载。可以使用 dbContext.Model.FindEntityType(typeof(T))。FindPrimaryKey()。Properties 来针对EF Core进行调整,以查找PK属性并构建谓词。另外,由于EF Core包含项更加复杂(需要 Include / ThenInclude 链),因此您可能会发现此线程有趣<一个href = https://stackoverflow.com/questions/38312437/can-a-string-based-include-alternative-be-created-in-entity-framework-core>是否可以在其中创建基于字符串的包含替代项实体框架核心?。


If I want to include related objects in an EF7 query, it's nice and easy:

var myThing = db.MyThings
                .Include(t => t.RelatedThing)
                .Where(t => t.SomeCondition == true)
                .ToList();

Also, there's a nice method on the DbSet<T> that makes it easy to load a single object by its key:

var myThing = db.MyThings.Find(thingId);

But now I want to load myThing by its Id, along with its RelatedThing. Unfortunately (and understandably) .Find() is a method of DbSet<T>, not IQueryable<T>. Obviously I could do this:

var myThing = db.MyThings
                .Include(t => t.RelatedThing)
                .SingleOrDefault(t => t.MyThingId == thingId);

But I specifically want to use the .Find() method, because it's nice and generic and I'm writing a method that generically loads a record along with "included" relationships specified by an Expression<Func<T, object>>.

Any suggestions how to do this?

解决方案

It was not possible with EF6, I don't think EF Core will change that. This is because the main purpose of the Find method is to bring the already loaded entity from the local cache, or load it from the database if it's not there. So the eager loading (Include) can be used only in the later case, while in the former it would need to perform explicit loading. Combining both in a single method might be technically possible, but is hard.

I think you should take the FirstOrDefault (or SingleOrDefault) route combined with eager loading. You can see a sample implementation for EF6 in Repository generic method GetById using eager loading. It could be adjusted for EF Core like using the dbContext.Model.FindEntityType(typeof(T)).FindPrimaryKey().Properties to find the PK properties and build the predicate. Also since EF Core includes are bit more complicated (require Include / ThenInclude chains), you might find interesting this thread Can a String based Include alternative be created in Entity Framework Core?.

这篇关于如何在DbSet.Find()中包含相关表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆