Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Error on IQuerable Extension and DBFunction #10236


User avatar
0
andmattia created
  • 4.8.1
  • Angular
  • .net core 2.2 / .net 4.6.1

Hi

we have an issue on abp and LINQ to SQL. We have create a JSON_VALUE function and it works fine until we try to call it from IQuerable static extension. Our project is very complex but I have create a empty project and add 2 DLL to reproduce the error. The step are:

  1. create 1 DLL (abp module or not is the same)

  2. create and abstract class to extend a AppService

  3. cerate a IQuerable extension to add a where with JSON_VALUE dbFunction

  4. if I call in base abstract class where I call ToList / ToArray I catch a NotImplemented Exception

  5. if I add the where directly on AppService and call ToList/ToArray the SQL will be translatend in correct way

            var query = from auditLog in _repository.GetAll()
                join user in _repositoryUser.GetAll() on auditLog.EditionId equals user.Id into userJoin
                from joinedUser in userJoin.DefaultIfEmpty()
                //where DemoEfCoreSqlJsonExtension.JsonValue(auditLog.TenancyName,"$") != null
                select new FullTenant { TenancyName = auditLog.TenancyName, Tenant = auditLog, Edition = joinedUser };
                
                var hjkh =  query.ToList();
                
                
                public static class DemoEfCoreSqlJsonExtension
    {
        [DbFunction("JSON_VALUE", "")]
        public static string JsonValue(string column, [NotParameterized] string path)
        {
            throw new NotSupportedException();
        }

        [DbFunction("JSON_QUERY", "")]
        public static string JsonQuery(string column, [NotParameterized] string path)
        {
            throw new NotSupportedException();
        }
    }
    
    public static class DemoQueryableExtensions
    {
        public static IQueryable<TGetAllListResult> DemoApplyExtraFieldFilter<TGetAllListResult>(
            this IQueryable<TGetAllListResult> query)
            where TGetAllListResult : class, IDynamicTelerikExtraFields
        {
            return Queryable.Where(query, x => DemoEfCoreSqlJsonExtension.JsonValue(x.TenancyName, "$") != null && Convert.ToDecimal(DemoEfCoreSqlJsonExtension.JsonValue(x.TenancyName, "$")) == 7);
        }
    }
    
    
    protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.HasDbFunction(() => DemoEfCoreSqlJsonExtension.JsonValue(default(string), default(string)));
            modelBuilder.HasDbFunction(() => DemoEfCoreSqlJsonExtension.JsonQuery(default(string), default(string)));

It wokrs only on AppService or Core Module but not from static Extension


3 Answer(s)
  • User Avatar
    0
    musa.demir created

    Can you please share the error message?

  • User Avatar
    0
    andmattia created

    Hi

    the error message is NotSupportedException because the Linq interpreter not call the SQL Json Function but call static class

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @andmattia

    I think this is not supported by EF Core. Were you able to run it on another app which is non-AspNet Zero ? As far as I know, you can't use such methods in your LINQ query.