Changing EFCore 1.1 Include and ThenInclude Mode

In EFCore 1.1 there is .Include

        var q = (from o in context.Company
                 where o.Id == CompanyId && !o.IsDelete
                 select o)
                .Include(x => x.Personnel)
                .ThenInclude(x => x.HomeAddress)
                .First();

But they .Includealso .ThenIncludeallow properties. I want to create an extension method that modifies the original behavior to exclude IsDelete (the database is a soft delete)

    public static IIncludableQueryable<TEntity, TProperty> AlongWith<TEntity, TProperty>(this IQueryable<TEntity> source, Expression<Func<TEntity, TProperty>> navigationPropertyPath) where TEntity : Domain.EntityBase
    {
        // original EFCore 1.1 .Include(..) behavior
        var result = source.Include(navigationPropertyPath);
        return result;
    }

My goal is to create an expression similar navigationPropertyPath, but enable validation for IsDelete

        // 1. Create the expression for IsDelete == false
        var type = typeof(Domain.EntityBase);
        ParameterExpression pe = Expression.Parameter(type, "personnel");
        var left = Expression.Property(pe, type.GetProperty("IsDelete"));
        var right = Expression.Constant(false);
        var predicateBody = Expression.Equal(left, right);

        // 2. Attempt to append the IsDelete check from step 1
        var lambda = Expression.Lambda(navigationPropertyPath.Body, pe);
        // error: System.InvalidOperationException
        var navigationPropertyPathWithDeleteFalse = Expression.And(lambda, predicateBody);              

Can I add && !IsDeletewhen for navigationPropertyPath?

+4
source share

Source: https://habr.com/ru/post/1694928/


All Articles