It makes sense that adding a class constraint will solve the problem.
When you write:
public virtual async Task<T> Get(int id) where T : IDataModel, new() { var connection = await GetConnection(); return await connection.Table<T>() .Where(item => item.Id == id) .FirstOrDefaultAsync(); }
You do not see this, but the compiler will insert a cast between item and item.Id
That is, what the compiler actually writes:
public virtual async Task<T> Get(int id) where T : IDataModel, new() { var connection = await GetConnection(); return await connection.Table<T>() .Where(item => ((IDataModel)item).Id == id) .FirstOrDefaultAsync(); }
This listing is inserted because it is necessary if T is a value type.
It is easy to imagine that the query provider for SQLite.net does not correctly process the entered data, since this is not trivial.
Adding a class constraint allows the compiler to avoid inserting this cast, which leads to a simpler expression that the SQLite.net query provider can apparently translate correctly.
Jean hominal
source share