Aurelius 2.9: Unexpected field type

I just tried new 2.9 version. It gives 'Unexpected field type Safir_ORM.TSO_Terminals' exception. 
I was OK with 2.7.1 version.  Exception raises in following many-to-many check:
   try
      Results := dmSOMA.SOMA.CreateCriteria<TSO_Organizations_x_Terminals>
                            .Select(TProjections.ProjectionList.Add(TProjections.count('TerminalID')))
                            .Add(TLinq.Eq('TerminalID', Terminal.TerminalID))
                            .Add(TLinq.Eq('OrganizationID', Organization.OrganizationID))
                            .Add(TLinq.Eq('RowIsActive', 1))
                            .ListValues;
      Result:=(results[0].Values[0]=1); //Count=1
   except
      result:=false;
   end;

Can you please provide more info? What is your mapping? Where does Sarif_ORM table comes from? Where does TSO_Terminals column comes from? 

In what line the error happens? Do you have the call stack and exact line in Aurelius where exception occurs? If you need to send some project files or screenshots, please send it to our support e-mail, thank you.

SafirORM is mapping unit produced by Data Modeler. 

Exception comes from function TMappingExplorer.ResolveFieldType in line 2320 

CallStack:
SafirException [TdmTerminalsController.isTerminalVisibleToOrganization] EUnexpectedFieldType raised, with message : Unexpected field type: SafirORM.TSO_Terminals.Safir CallStack:
(0051FECE){SafirSpider.exe} [0091FECE] Aurelius.Mapping.Explorer.TMappingExplorer.ResolveFieldType (Line 2320, "Aurelius.Mapping.Explorer.pas") + $1A
(0049B4B8){SafirSpider.exe} [0089B4B8] Aurelius.Commands.Selecter.TSelecter.ReadProjectionsObject (Line 871, "Aurelius.Commands.Selecter.pas") + $11
(0049B9D5){SafirSpider.exe} [0089B9D5] Aurelius.Commands.Selecter.TSelecter.SelectFetch (Line 954, "Aurelius.Commands.Selecter.pas") + $1F
(004CFAC6){SafirSpider.exe} [008CFAC6] Aurelius.Engine.ObjectManager.TObjectManager.TEngineCursor.Next (Line 1774, "Aurelius.Engine.ObjectManager.pas") + $6
(004CCBE6){SafirSpider.exe} [008CCBE6] Aurelius.Engine.ObjectManager.TObjectManager.List (Line 599, "Aurelius.Engine.ObjectManager.pas") + $5
(00480CAB){SafirSpider.exe} [00880CAB] Aurelius.Criteria.Base.TCriteria.FillList (Line 751, "Aurelius.Criteria.Base.pas") + $13
(004904F5){SafirSpider.exe} [008904F5] Aurelius.Criteria.Base.TCriteria.List<Aurelius.Criteria.Base.TCriteriaResult> (Line 819, "System.Generics.Collections.pas") + $8
(0049053B){SafirSpider.exe} [0089053B] Aurelius.Criteria.Base.TCriteria.List<Aurelius.Criteria.Base.TCriteriaResult> (Line 827, "System.Generics.Collections.pas") + $9
(00480FF1){SafirSpider.exe} [00880FF1] Aurelius.Criteria.Base.TCriteria.ListValues (Line 836, "Aurelius.Criteria.Base.pas") + $3
(00986053){SafirSpider.exe} [00D86053] TerminalsController.TdmTerminalsController.isTerminalVisibleToOrganization (Line 283, "TerminalsController.pas") + $C1


TSO_Terminals mapping:
  [Entity]
  [Table('Terminals')]
  [UniqueKey('NetworkAddress, TerminalType')]
  [Id('FTerminalID', TIdGenerator.IdentityOrSequence)]
  TSO_Terminals = class
  private
    [Column('TerminalID', [TColumnProp.Required, TColumnProp.NoInsert, TColumnProp.NoUpdate])]
    FTerminalID: integer;
    
    [Column('TerminalName', [TColumnProp.Required], 100)]
    FTerminalName: string;
    
    [Column('TerminalType', [TColumnProp.Required])]
    FTerminalType: integer;
    
    [Column('Description', [], 255)]
    FDescription: Nullable<string>;
    
    [Column('NetworkAddress', [TColumnProp.Required], 100)]
    FNetworkAddress: string;
    
    [Column('MessageProtocol', [])]
    FMessageProtocol: Nullable<integer>;
    
    [Column('MessageProtocolSpecifier', [], 50)]
    FMessageProtocolSpecifier: Nullable<string>;
    
    [Column('TerminalPriority', [TColumnProp.Required])]
    FTerminalPriority: integer;
    
    [Column('isPatientMonitor', [TColumnProp.Required])]
    FisPatientMonitor: integer;
    
    [Column('isVDITerminal', [TColumnProp.Required])]
    FisVDITerminal: integer;
    
    [Column('isFixedInstallation', [TColumnProp.Required])]
    FisFixedInstallation: integer;
    
    [Column('isIndividualAddress', [TColumnProp.Required])]
    FisIndividualAddress: integer;
    
    [Column('isCenter', [TColumnProp.Required])]
    FisCenter: integer;
    
    [Column('isGroupTerminalAddress', [TColumnProp.Required])]
    FisGroupTerminalAddress: integer;
    
    [Column('isSubcribeGroupAddress', [TColumnProp.Required])]
    FisSubcribeGroupAddress: integer;
    
    [Column('isPositionMessaging', [TColumnProp.Required])]
    FisPositionMessaging: integer;
    
    [Column('isStatusMessaging', [TColumnProp.Required])]
    FisStatusMessaging: integer;
    
    [Column('isFastMessaging', [TColumnProp.Required])]
    FisFastMessaging: integer;
    
    [Column('isTaskMessaging', [TColumnProp.Required])]
    FisTaskMessaging: integer;
    
    [Column('isReceiveOnly', [TColumnProp.Required])]
    FisReceiveOnly: integer;
    
    [Column('isActive', [TColumnProp.Required])]
    FisActive: integer;
    
    [Column('InternalTag', [], 50)]
    FInternalTag: Nullable<string>;
    
    [Column('RowIsActive', [TColumnProp.Required])]
    FRowIsActive: integer;
    
    [Column('RowLastModified', [TColumnProp.Required])]
    FRowLastModified: TDateTime;
    
    [Association([TAssociationProp.Lazy, TAssociationProp.Required], [])]
    [JoinColumn('UnitID', [TColumnProp.Required], 'UnitID')]
    FUnitID: Proxy<TSO_Units>;
    function GetUnitID: TSO_Units;
    procedure SetUnitID(const Value: TSO_Units);
  public
    property TerminalID: integer read FTerminalID write FTerminalID;
    property TerminalName: string read FTerminalName write FTerminalName;
    property TerminalType: integer read FTerminalType write FTerminalType;
    property Description: Nullable<string> read FDescription write FDescription;
    property NetworkAddress: string read FNetworkAddress write FNetworkAddress;
    property MessageProtocol: Nullable<integer> read FMessageProtocol write FMessageProtocol;
    property MessageProtocolSpecifier: Nullable<string> read FMessageProtocolSpecifier write FMessageProtocolSpecifier;
    property TerminalPriority: integer read FTerminalPriority write FTerminalPriority;
    property isPatientMonitor: integer read FisPatientMonitor write FisPatientMonitor;
    property isVDITerminal: integer read FisVDITerminal write FisVDITerminal;
    property isFixedInstallation: integer read FisFixedInstallation write FisFixedInstallation;
    property isIndividualAddress: integer read FisIndividualAddress write FisIndividualAddress;
    property isCenter: integer read FisCenter write FisCenter;
    property isGroupTerminalAddress: integer read FisGroupTerminalAddress write FisGroupTerminalAddress;
    property isSubcribeGroupAddress: integer read FisSubcribeGroupAddress write FisSubcribeGroupAddress;
    property isPositionMessaging: integer read FisPositionMessaging write FisPositionMessaging;
    property isStatusMessaging: integer read FisStatusMessaging write FisStatusMessaging;
    property isFastMessaging: integer read FisFastMessaging write FisFastMessaging;
    property isTaskMessaging: integer read FisTaskMessaging write FisTaskMessaging;
    property isReceiveOnly: integer read FisReceiveOnly write FisReceiveOnly;
    property isActive: integer read FisActive write FisActive;
    property InternalTag: Nullable<string> read FInternalTag write FInternalTag;
    property RowIsActive: integer read FRowIsActive write FRowIsActive;
    property RowLastModified: TDateTime read FRowLastModified write FRowLastModified;
    property UnitID: TSO_Units read GetUnitID write SetUnitID;
  end;

You didn't provide me the mapping of class TSO_Organizations_x_Terminals so I'd have to guess, but I think the problem is with this line:


.Select(TProjections.ProjectionList.Add(TProjections.count('TerminalID')))

I suppose "TerminalID" is of type TSO_Terminals, which is an object. You can't count on objects, you must count scalar values. The same for the "TLinq.Eq", you must create aliases to the related associations and check for them "Id" properties.
I forgot to provide that mapping but your guess was correct. I thought also it might have something to do with that object reference but it have been working before.Maybe this have something to do with better error messages.

Now I tried to create alias but it is also giving exception EPropertyNotFound raised, with message : Property "TerminalID" not found on class "TSO_Terminals":

      Results := dmSOMA.SOMA.CreateCriteria<TSO_Organizations_x_Terminals>
                            .CreateAlias('TerminalID','Terminal', TFetchMode.Eager)
                            .CreateAlias('Terminal.TerminalID','iTerminalID', TFetchMode.Eager)
                            //.CreateAlias('OrganizationID','Organization', TFetchMode.Eager)
                            //.CreateAlias('Organization.OrganizationID','iOrganizationID', TFetchMode.Eager)
                            .Select(TProjections.ProjectionList.Add(TProjections.count('iTerminalID')))
                            .Add(TLinq.Eq('iTerminalID', Terminal.TerminalID))
                            .Add(TLinq.Eq('OrganizationID', Organization.OrganizationID))
                            .Add(TLinq.Eq('RowIsActive', 1))
                            .ListValues;

Exception comes from:
(0051A8E8){SafirSpider.exe} [0091A8E8] Aurelius.Mapping.Explorer.TMappingExplorer.GetAssociationByPropertyName (Line 858, "Aurelius.Mapping.Explorer.pas") + $1E

First I tried one alias but i does not help either:
    .CreateAlias('TerminalID.TerminalID','iTerminalID', TFetchMode.Eager)

  [Entity]
  [Table('Organizations_x_Terminals')]
  [Id('FOrganizationID', TIdGenerator.None)]
  [Id('FTerminalID', TIdGenerator.None)]
  TSO_Organizations_x_Terminals = class
  private
    [Column('RowIsActive', [TColumnProp.Required])]
    FRowIsActive: integer;
    
    [Column('RowLastModified', [TColumnProp.Required])]
    FRowLastModified: TDateTime;
    
    [Association([TAssociationProp.Lazy, TAssociationProp.Required], [])]
    [JoinColumn('OrganizationID', [TColumnProp.Required], 'OrganizationID')]
    FOrganizationID: Proxy<TSO_Organizations>;
    
    [Association([TAssociationProp.Lazy, TAssociationProp.Required], [])]
    [JoinColumn('TerminalID', [TColumnProp.Required], 'TerminalID')]
    FTerminalID: Proxy<TSO_Terminals>;
    function GetOrganizationID: TSO_Organizations;
    procedure SetOrganizationID(const Value: TSO_Organizations);
    function GetTerminalID: TSO_Terminals;
    procedure SetTerminalID(const Value: TSO_Terminals);
  public
    property RowIsActive: integer read FRowIsActive write FRowIsActive;
    property RowLastModified: TDateTime read FRowLastModified write FRowLastModified;
    property OrganizationID: TSO_Organizations read GetOrganizationID write SetOrganizationID;
    property TerminalID: TSO_Terminals read GetTerminalID write SetTerminalID;
  end;

You don't need to create an alias for scalar properties, you should use it directly:

remove this:

.CreateAlias('Terminal.TerminalID','iTerminalID', TFetchMode.Eager)

and use this:

.Select(TProjections.ProjectionList.Add(TProjections.count('Terminal.TerminalID')))

Yes, It works  that way.

Thanks Wagner.