簡體   English   中英

實體框架存儲庫不適用於多層.Include()

[英]Entity Framework Repository Not Working with Multi-Level .Include()

我正在處理一個大型項目,其中其他人使用Entity Framework設置了Repository類。 我意識到某些問題可能與自定義代碼有關,但是有人可以幫助我確定下一個內容嗎?

我正在嘗試編寫此查詢:

List<ProductItem> addedProductItems =
    repository.Query<ProductItem>()
        .Include(pi => pi.Product)
        .Include(pi => pi.ProductItemVendors.Select(v => v.ProductPricings))
        .Where(pi => !pi.IsDeleted && productIdSortOrder.Keys.Contains(pi.ProductId))
        .AsEnumerable()
        .OrderBy(pi => productIdSortOrder[pi.ProductId])
        .ToList();

但是帶有第二個.Include()會產生以下錯誤。 (第一個.Include()正常工作。):

錯誤5類型'System.Collections.Generic.IEnumerable>'不能用作通用類型或方法'VIP.Domain.Repository.IRepositoryObjectQuery.Include(System.Linq.Expressions.Expression>)'中的類型參數'TEntity2' 。 沒有從“ System.Collections.Generic.IEnumerable>”到“ Leo.Domain.ILeoDomainModelItem”的隱式引用轉換。

這是定義包括Include()的接口的方式:

public interface IRepositoryObjectQuery<TEntity, TEntityMarker>
    : IOrderedQueryable<TEntity>, IQueryable<TEntity>, IEnumerable<TEntity>, IOrderedQueryable, IQueryable, IEnumerable, IListSource
    where TEntity : TEntityMarker
{
    IRepositoryObjectQuery<TEntity, TEntityMarker> Include<TEntity2>(IRepositoryPropertyChain<TEntity, TEntity2> propertyChain) where TEntity2 : TEntityMarker;
    IRepositoryObjectQuery<TEntity, TEntityMarker> Include<TEntity2>(Expression<Func<TEntity, TEntity2>> propertyGetter) where TEntity2 : TEntityMarker;
    IRepositoryObjectQuery<TEntity, TEntityMarker> Include<TEntity2>(Expression<Func<TEntity, ICollection<TEntity2>>> propertyGetter) where TEntity2 : TEntityMarker;
}

有人可以看到這里的問題嗎?

表達式的結果

pi => pi.ProductItemVendors.Select(v => v.ProductPricings)

不是ICollection<T>並且可能不是IRepositoryPropertyChain ,因此可以使用它的唯一重載是

IRepositoryObjectQuery<TEntity, TEntityMarker> Include<TEntity2>(
    Expression<Func<TEntity, TEntity2>> propertyGetter) 
    where TEntity2 : TEntityMarker;

由於類型約束, TEntity2必須派生自/實現TEntityMarker (顯然是Leo.Domain.ILeoDomainModelItem )。 IEnumerable<TypeOfProductPricings> (包含表達式的結果)未實現該接口。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM