简体   繁体   中英

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

I'm working with a large project where someone else set up the Repository classes using Entity Framework. I realize some of the issue may be related to custom code, but could anyone help me determine where to look next?

I'm trying to write this query:

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();

But the line with the second .Include() produces the following error. (The first .Include() works fine.):

Error 5 The type 'System.Collections.Generic.IEnumerable>' cannot be used as type parameter 'TEntity2' in the generic type or method 'VIP.Domain.Repository.IRepositoryObjectQuery.Include(System.Linq.Expressions.Expression>)'. There is no implicit reference conversion from 'System.Collections.Generic.IEnumerable>' to 'Leo.Domain.ILeoDomainModelItem'.

This is how the interface that includes Include() is defined:

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;
}

Could anyone see what might be the issue here?

The result of the expression

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

is not an ICollection<T> and presumably not an IRepositoryPropertyChain , so the only overload in which it may be used is

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

Due to the type constraint, TEntity2 must be derived from/implement TEntityMarker (which is apparently Leo.Domain.ILeoDomainModelItem ). IEnumerable<TypeOfProductPricings> (the result of the include expression) does not implement that interface.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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