Skip to content

Error: Invalid expression. The property <Id> is not defined on a target type  #364

@eMTeTeam

Description

@eMTeTeam

I have an Interface with base fields

public interface IWithId
    {
        /// <summary>
        /// Gets or sets the primary id.
        /// </summary>
        string Id { get; set; }
    }

Inherited class:

[Map("ACCOUNTS")]
public class Account : IWithId
    {
        /// <summary>
        /// Gets or sets the account primary id.
        /// </summary>
        [Map("AccountID")]
        public string Id { get; set; }

        /// <summary>
        /// Gets or sets the account name.
        /// </summary>
        [Map("AccountName")]
        public string Name { get; set; }
    }

I have a Generic Repository class. Currently I'm trying to get the Account information By Id using below code,

public abstract class RepositoryBase<TModel> : where TModel : class, IWithId, new()
{

public async Task<TModel> ReadByIdAsync(string id, CancellationToken token)
        {
            var model = await QueryAsync(entity => entity.Id == id);
            return model.FirstOrDefault();
        }
}

In above I’m getting this error

RepoDb.Exceptions.InvalidExpressionException: 'Invalid expression '(entity.Id == value(eMTe.Expense.Repository.Implementation.RepositoryBase1+<>c__DisplayClass7_0[eMTe.Expense.Model.Account]).id)'. The property Id is not defined on a target type 'eMTe.Expense.Model.Account'.'

Expectation is Id should be resolved to get the Account class information and ‘AccountID’ should be added in where class. But the Expression field is resolving as IWithId Interface and no Map attribute is resolved.
This is the reason for this error.

I did some analysis and issue is happening in below code

public class QueryField

var field = expression.GetField();
            if (PropertyCache.Get<TEntity>().Any(property => string.Equals(PropertyMappedNameCache.Get(property.PropertyInfo), field.Name, StringComparison.OrdinalIgnoreCase)) == false)
            {
                throw new InvalidExpressionException($"Invalid expression '{expression.ToString()}'. The property {field.Name} is not defined on a target type '{typeof(TEntity).FullName}'.");
            }

Line No: 212
The _expression.GetField();_ method returns ‘Id’ but not ‘AccountID

This stackoverflow talks about Get overridden property attribute and the exact scenario I’m facing.
https://stackoverflow.com/questions/20835132/get-overridden-property-attribute

Please suggest a solution for this.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingdeployedFeature or bug is deployed at the current releasefixedThe bug, issue, incident has been fixed.priorityTop priority feature or things to dotodoThings to be done in the future

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions