servicestack-autoquery

ServiceStack AutoQuery crash on synthetic field


This is a follow up on: ServiceStack AutoQuery synthetic field

.NET Core empty web template on newest 5.x versions of SS and .Net Core.

I'm trying to create an AutoQuery service that I can decorate with a few synthetic fields (i.e. fields that don't come from the database). So I've got this AutoQuery service with the following Data Transfer Object:

public class DataSource {
    [Ignore]
    public string Hello { get { return "Hello there"; }}
    public string DataSourceId { get; set; }    
    public string DataSourceName { get; set; }  
    public int DataSourceSort { get; set; }     
    public bool DataSourceDisabled { get; set; }
    public DateTime LastModified { get; set; }  
}

When I don't include the public string Hello it all works. The service itself is just a on-liner:

[Route("/query/datasources")]
public class QueryDatasources : QueryDb<DataSource> {}

This is the error I'm getting:

Offset0Total0Response Status Error CodeInvalidOperationExceptionMessageSequence contains no matching elementStack Trace[QueryDatasources: 22/03/2019 17:47:41]: [REQUEST: {}] System.InvalidOperationException: Sequence contains no matching element at System.Linq.Enumerable.First[TSource](IEnumerable'1 source, Func'2 predicate) at ServiceStack.OrmLite.OrmLiteDialectProviderBase'1.GetLoadChildrenSubSelect[From](SqlExpression'1 expr) in C:\BuildAgent\work\27e4cc16641be8c0\src\ServiceStack.OrmLite\OrmLiteDialectProviderBase.cs:line 1616 at ServiceStack.OrmLite.SqlServer.SqlServerOrmLiteDialectProvider.GetLoadChildrenSubSelect[From](SqlExpression'1 expr) in C:\BuildAgent\work\27e4cc16641be8c0\src\ServiceStack.OrmLite.SqlServer\SqlServerOrmLiteDialectProvider.cs:line 587 at ServiceStack.OrmLite.Support.LoadList'2..ctor(IDbCommand dbCmd, SqlExpression'1 q) in C:\BuildAgent\work\27e4cc16641be8c0\src\ServiceStack.OrmLite\Support\LoadList.cs:line 46 at ServiceStack.OrmLite.OrmLiteReadCommandExtensions.LoadListWithReferences[Into,From](IDbCommand dbCmd, SqlExpression'1 expr, IEnumerable'1 include) in C:\BuildAgent\work\27e4cc16641be8c0\src\ServiceStack.OrmLite\OrmLiteReadCommandExtensions.cs:line 957 at ServiceStack.OrmLite.OrmLiteExecFilter.Exec[T](IDbConnection dbConn, Func'2 filter) in C:\BuildAgent\work\27e4cc16641be8c0\src\ServiceStack.OrmLite\OrmLiteExecFilter.cs:line 64 at ServiceStack.TypedQuery'2.Execute[Into](IDbConnection db, ISqlExpression query) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack.Server\AutoQueryFeature.cs:line 1089


Solution

  • The issue is because your data model doesn't have an explicit Primary Key.

    In OrmLite each model must have a single primary key, by convention OrmLite expects it to be named Id and you can use [Alias("DbFieldName")] attribute it map it to a column with a different name or use the [PrimaryKey] attribute to tell OrmLite to use a different property for the primary key.

    If your model doesn't have an explicit [PrimaryKey] attribute it will use the first [AutoIncrement] or [AutoId] on your model, if none of these exists it fallsback to using the first property as the primary key in which case is an ignored property.

    I've removed ignored properties from consideration in this commit, however you should be explicit in which field to use as your Primary Key (using any of the above attributes or Id naming convention) instead of relying on OrmLite's fallback heuristic.