无法解析表达式'...:给定的参数与预期的参数不匹配”

时间:2018-09-08 12:48:43

标签: c# asp.net-core .net-core entity-framework-core

我正在使用framewok core 2.0实体的CompiledQueries功能,但是即使它不给出语法错误,但在程序运行时却出现此异常:

“无法解析表达式'值(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1 [TaskManager.Entities.EntitiesDB.USER])AsNoTracking()。其中​​(__条件)':给定的参数与预期参数:无法将类型为“ System.Linq.Expressions.TypedParameterExpression”的对象转换为类型为“ System.Linq.Expressions.LambdaExpression”。“

我不知道这是什么。我需要帮助。

这是我的存储库的源代码,它显示此错误:

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

我实现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);
    }
}

我的数据库实体:

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

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

尝试将其分解。

从一个简单的测试用例开始(例如一些虚拟new UserResponseDTO),然后尝试一个简单的静态选择(选择一个特定用户),然后添加if / else条件,并尝试使用和不使用{{1 }},然后在第一个if / else分支中添加一个简单的静态选择,然后在第二个分支中执行相同的操作,直到获得您现在拥有的语句。

您还可以先进行选择(不使用if / else),以检查问题是否在Context定义中。也许在包含conditions" nextval('\"S_SEQUSER\"') "的行之一中。 btw:为什么这里有空格?

介于两者之间的某些地方会出问题。区别在于:现在您知道;-)

在哪里

绝对要为此编写一个单元测试。通常只需单击鼠标右键即可。