Page 1 of 1

DbSet Include Generates incorrect SQL

Posted: Thu 18 Jan 2018 18:20
by Simonare

Code: Select all

public virtual async Task<TEntity> GetByIdAsync(object[] keyValues,
        List<Expression<Func<TEntity, object>>> includes,
        CancellationToken cancellationToken = default(CancellationToken))
    {
        Task<TEntity> model = null;

        foreach (var include in includes)
        {
            if (include.Body.Type.GetInterface(nameof(IEnumerable<TEntity>)) != null)
            { //this part generates SQL Below
                await DbSet.Include(include).LoadAsync(cancellationToken);
                model = DbSet.FindAsync(keyValues, cancellationToken);
            }
            else //this part is working
            {
                var propertyName = ((MemberExpression)include.Body).Member.Name;
                model = DbSet.FindAsync(keyValues, cancellationToken);
                await DbContext.Entry(model.Result).Navigation(propertyName).LoadAsync(cancellationToken);
            }
        }

        if (model == null)
            model = DbSet.FindAsync(keyValues, cancellationToken);

        return await model;
    }
above code returns incorrect SQL which is specified below:

Code: Select all

SELECT "product.MhpProducts".MP_MHP_ID, "product.MhpProducts".MP_PRODUCT_ID, "product.MhpProducts".MP_G_ORDER, "product.MhpProducts".G_END_DATE, "product.MhpProducts".G_INSERT_BY, "product.MhpProducts".G_INSERT_DATE, "product.MhpProducts".G_IS_DELETED, "product.MhpProducts".G_START_DATE, "product.MhpProducts"."MP_WEST_CORE._DOMAIN._ENTITIES._WEST_LIFE._MHP_PRODUCT" FROM MHP_PRODUCT "product.MhpProducts" INNER JOIN ( SELECT "product0".TP_ID FROM TREE_PRODUCT "product0" ) "t" ON "product.MhpProducts".MP_PRODUCT_ID = "t".TP_ID ORDER BY "t".TP_ID
Any Idea about it?

it seems like a Bug

Re: DbSet Include Generates incorrect SQL

Posted: Fri 19 Jan 2018 16:49
by Shalex
Please upload a small test project with the corresponding DDL/DML script to ftp://ftp.devart.com (credentials: anonymous / yourEmail).