2016-09-21 93 views
2

我正在使用EntityFramework核心,并试图加载仅存在于某种派生类型(全部在单个查询中)的导航属性。 可能最好用一个简单的例子来演示。EntityFramework核心:渴望加载派生类型的导航属性

假设你有一个像

class Transaction 
{ 
    public Product product { get; set; } 
    public DateTime date { get; set; } 
} 

abstract class Product 
{ 
    public string Name { get; set; } 
} 

class PhysicalProduct : Product 
{ 
    public Photo photo { get; set; } 
} 

class Service : Product 
{ 
    public Person provider { get; set; } 
} 

而且有些的DbContext

class MyContext : DbContext 
{ 
    public DbSet<Transaction> Transactions; 
} 

一些数据结构如何可以查询MyContext.Transactions返回的所有交易,并包括(贪婪加载)Transaction.product。照片(如果产品是PhysicalProduct)和Transaction.product.provider(如果产品是服务)?如上所述,试图用一个查询来实现这一点。

我已经试过如下:

// This is conceptually what I want to achieve. 
// Not very surprisingly, this will throw an InvalidCastException 
Transactions 
    .Include(x => ((PhysicalProduct)x.product).photo) 
    .Include(x => ((Service)x.product).provider) 
    .ToList(); 

// Based on http://stackoverflow.com/questions/7635152/entity-framework-eager-loading-of-subclass-related-objects 
// Projection into an anonymous type, then transform back. 
// doesn't work though, throws an InvalidOperationException, e.g. 
// The property "photo" on entity type "Product" could not be found. Ensure that the property exists and has been included in the model. 
// i.e. even though I wrapped this in a condition (x.product is PhysicalProduct), seems like EntityFramework still tries to execute or parse the statement thereafter even if the condition is not true. 
var query = Transactions.Select(x => new 
{ 
    _transaction = x, 
    _physicalProductPhoto = (x.product is PhysicalProduct) ? ((PhysicalProduct)x.product).photo : null; 
    _serviceProvider = (x.product is Service) ? ((Service)x.product).provider : null; 
}) 
.ToList() // Execute query. Exception will be thrown at this step. 
.Select(x => 
{ 
    var result = x._transaction; 

    if (x.product is PhysicalProduct) 
    ((PhysicalProduct)x.product).photo = x._physicalProductPhoto; 
    else if(x.product is Service) 
    ((Service)x.product).provider = x._serviceProvider; 

    return result; 
}) 
.ToList(); 

任何人都可以想办法来实现这一目标? 谢谢!

回答

3

昨天我在EF6战斗类似的问题 - EF Eager fetching derived class。目前EF Core在这方面并没有更好 - 事实上它更糟糕,因为从3 EF6的解决方法,只有#2在这里工作。

解决方法是:

绝对不能用单个查询来完成。您需要执行主查询并在内存中实现结果。然后,对于每个派生的导航类型,收集PK并执行由这些键过滤的查询。最后,由于EF导航属性修正,您将最终加载所有导航属性。

var transactions = db.Transactions.Include(e => e.product).ToList(); 

var productIds = transactions.Where(e => e.product is PhysicalProduct) 
    .Select(e => e.product.Id).Distinct(); 
db.BaseProducts.OfType<PhysicalProduct>().Include(e => e.photo) 
    .Where(e => productIds.Contains(e.Id)).Load(); 

var serviceIds = transactions.Where(e => e.product is Service) 
    .Select(e => e.product.Id).Distinct(); 
db.BaseProducts.OfType<Service>().Include(e => e.provider) 
    .Where(e => serviceIds.Contains(e.Id)).Load(); 
+0

谢谢伊万!不幸的是,这可能会使EF的大部分IAsyncEnumerable优势无效;加上它让我头痛,因为我无法执行并将密钥存入内存,这似乎与EFCore和GUID-Keys一起生成错误的SQL,所以需要像在您的示例中一样将IID保留为IQueryable(productIds ) - 尽管如此,多层次案例的一个障碍,例如包括本身属于派生类型的属性的派生类型的引用。无论如何,希望我能弄明白这一点;感谢您的意见,这非常有帮助! – Bogey

2

EF Core尚未支持此功能。跟踪它的问题请参阅https://github.com/aspnet/EntityFramework/issues/3910

我相信唯一的解决方法是执行多个查询,并让EF上下文为您做修复。

+0

感谢亚瑟。在EFCore中,这样一个关键性的功能并没有实现。解决方法是导致一些问题(回复伊万的帖子中描述)不幸,但似乎这是唯一的方式去,然后 – Bogey

-1

我相信通过使用“ThenInclude”的链接文档。你可以做到这一点。对不起,但我没有自己尝试,所以我无法验证它的工作原理。

var blogs = context.Blogs 
.Include(blog => blog.Posts) 
    .ThenInclude(post => post.Author) 
    .ThenInclude(author => author.Photo) 
.Include(blog => blog.Owner) 
    .ThenInclude(owner => owner.Photo) 
.ToList(); 

https://docs.efproject.net/en/latest/querying/related-data.html

+0

谢谢。我相信。然后插入是用于子引用(如你的例子),但不是为了铸造类型。即如果Blogs.Posts如果键入Post,并且DerivedPost是派生的Post,则不允许包含任何特定于DerivedPost的属性 – Bogey