Could not parse expression '…: The given arguments did not match the expected arguments”

I am using the CompiledQueries feature of the framewok core 2.0 entity but even though it does not give syntax error, when the program is running, I get this exception:

"Could not parse expression 'value (Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1 [TaskManager.Entities.EntitiesDB.USER]) AsNoTracking (). Where (__ conditions)': The given arguments did not match the expected arguments: Object of type 'System.Linq.Expressions.TypedParameterExpression' can not be converted to type 'System.Linq.Expressions.LambdaExpression'. "

I have no idea what it is. I Need help.

Here is the source code of my repository which displays this error:

    /// <summary>
    /// Query of user
    /// </summary>
    /// <param name="_context">context</param>
    /// <param name="conditions">conditions for clause "Where"</param>
    /// <returns>first task that meets the conditions or null </returns>
    private static Func<Context, Func<USER, bool>, Task<UserResponseDTO>> 
           _getUser = EF.CompileAsyncQuery (
               (Context _context, Func<USER, bool> conditions) =>
                  (conditions == null)? 
                      _context.USER.Select(user =>
                          new UserResponseDTO {
                            NAME = user.NAME,
                            EMAIL = user.EMAIL,
                            SEQUSER = user.SEQUSER,
                            DATEREGISTER = user.DATEREGISTER
                          }
                       ).AsNoTracking().FirstOrDefault ()
                  :                                        
                      _context.USER.AsNoTracking()
                      .Where(conditions).Select(user =>
                          new UserResponseDTO {
                            NAME = user.NAME,
                            EMAIL = user.EMAIL,
                            SEQUSER = user.SEQUSER,
                            DATEREGISTER = user.DATEREGISTER
                          }
                       ).FirstOrDefault ()
           );

my class that implements DbContext :

public class Context: DbContext
{
    public Context(DbContextOptions<Context> options): base(options){ }
    public virtual DbSet<USER> USER { get; set; }
    public virtual DbSet<TASK> TASK { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder){
        #region Sequences

        modelBuilder.HasSequence<long> 
        ("S_SEQUSER").StartsAt(1).HasMin(1);
        modelBuilder.HasSequence<long> 
        ("S_SEQTASK").StartsAt(1).HasMin(1);                   

        #endregion

        #region propeties Mapping
        modelBuilder.Entity<USER>()
            .Property(u => u.SEQUSER)                
            .HasDefaultValueSql(" nextval('\"S_SEQUSER\"') ");

        modelBuilder.Entity<TASK>()
           .Property(u => u.SEQTASK)            
           .HasDefaultValueSql(" nextval('\"S_SEQTASK\"') ");

        modelBuilder.Entity<USER>()
        .HasMany(u => u.TASK)
        .WithOne(u => u.USER);   
        #endregion        

        base.OnModelCreating(modelBuilder);
    }
}

my database entity:

/// <summary>
/// query user
/// </summary>
[Table(@"USER")]
public class USER
{
    #region Properties
    /// <summary>
    /// sequence of user
    /// </summary>
    /// <value></value>
    [Key()]
    [Column(@"SEQUSER")]
    public virtual long SEQUSER { get; set; }

    /// <summary>
    /// email of user
    /// </summary>
    /// <value></value>
    [Required]
    [StringLength(120)]
    [Column(@"EMAIL")]
    public virtual string EMAIL { get; set; }

    /// <summary>
    /// password of user
    /// </summary>
    /// <value></value>
    [Required]
    [StringLength(15)]
    [Column(@"PASSWORD")]
    public virtual string PASSWORD { get; set; }

    /// <summary>
    /// name of user
    /// </summary>
    /// <value></value>
    [Required]
    [StringLength(120)]
    [Column(@"NAME")]
    public virtual string NAME { get; set; }        

    /// <summary>
    /// date of user was registered
    /// </summary>
    /// <value></value>
    [Column(@"DATEREGISTER")]
    public virtual DateTime DATEREGISTER { get; set; }

    /// <summary>
    /// last acess of user in application
    /// </summary>
    /// <value></value>
    [Column(@"LASTACESS")]
    public virtual DateTime LASTACESS { get; set; }

    /// <summary>
    /// list of task 
    /// </summary>
    /// <value></value>
    [InverseProperty("USER")]
    public virtual List<TASK> TASK { get; set; }

    #endregion    
}

Could someone give me a hand in this?

Upvotes: 1

Views: 1304

Answers (1)

Xan-Kun Clark-Davis
Xan-Kun Clark-Davis

Reputation: 2843

Try breaking it down.

Start from a simple test-case (like some dummy new UserResponseDTO), then try with a simple static select (select one specific user), then add the if/else condition and try with and without conditions, then add a simple static select to your first if/else branch, then do the same to the second branch, until you get to the statement you have right now.

You could also do a select (without if/else) first, to check if your problem is in the Context definition. Maybe in one of the lines containing " nextval('\"S_SEQUSER\"') " or " nextval('\"S_SEQTASK\"') ". btw: Why the spaces here?

Somewhere inbetween, something will go wrong. The difference is: Now you know where ;-)

And definitely write a unit-test for that. It's usually just a right-mouse-click away.

Upvotes: 1

Related Questions