RepoDB: Error: Invalid expression. The property is not defined on a target type
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.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 15 (10 by maintainers)
Commits related to this issue
- RepoDb v1.10.4-beta1 | RepoDb v1.0.3-beta1 | Error: Invalid expression. The property <Id> is not defined on a target type #364 - MySql Support - Error: Invalid expression. The property <Id> is not de... — committed to mikependon/RepoDB by mikependon 4 years ago
- Unit Tests Guard for (Error: Invalid expression. The property <Id> is not defined on a target type #364) — committed to mikependon/RepoDB by mikependon 4 years ago
- Error: Invalid expression. The property <Id> is not defined on a target type #364 — committed to mikependon/RepoDB by mikependon 4 years ago
I will issue a fix for this. It is beyond your control as a consumer. It should be handled within RepoDb. I will do issue a beta for next version for you. Currently, I am running all Integration Tests.
Btw, it is a core change for the Expression Trees - so I need to revalidate all test-cases are passing before issuing a new version. I am almost done!