You made me think that maybe I should try to do a "Refresh from Relational Database", so I did that, and the llblgen project file was modified as follows:
Before:
<TypedViewDefinition Name="OrdersQry">
<Fields>
<Field Name="Address" Type="string" IsOptional="true" MaxLength="60" />
<Field Name="City" Type="string" IsOptional="true" MaxLength="15" />
<Field Name="CompanyName" Type="string" MaxLength="40" />
<Field Name="Country" Type="string" IsOptional="true" MaxLength="15" />
<Field Name="CustomerId" Type="string" IsOptional="true" MaxLength="5" />
<Field Name="EmployeeId" Type="int" IsOptional="true" Precision="10" />
<Field Name="Freight" Type="decimal" IsOptional="true" Precision="19" Scale="4" />
<Field Name="OrderDate" Type="datetime" IsOptional="true" />
<Field Name="OrderId" Type="int" Precision="10" />
<Field Name="PostalCode" Type="string" IsOptional="true" MaxLength="10" />
<Field Name="Region" Type="string" IsOptional="true" MaxLength="15" />
<Field Name="RequiredDate" Type="datetime" IsOptional="true" />
<Field Name="ShipAddress" Type="string" IsOptional="true" MaxLength="60" />
<Field Name="ShipCity" Type="string" IsOptional="true" MaxLength="15" />
<Field Name="ShipCountry" Type="string" IsOptional="true" MaxLength="15" />
<Field Name="ShipName" Type="string" IsOptional="true" MaxLength="40" />
<Field Name="ShippedDate" Type="datetime" IsOptional="true" />
<Field Name="ShipPostalCode" Type="string" IsOptional="true" MaxLength="10" />
<Field Name="ShipRegion" Type="string" IsOptional="true" MaxLength="15" />
<Field Name="ShipVia" Type="int" IsOptional="true" Precision="10" />
</Fields>
After:
<TypedViewDefinition Name="OrdersQry">
<Fields>
<Field Name="Address" Type="string" IsOptional="true" MaxLength="60" FieldIndex="15" />
<Field Name="City" Type="string" IsOptional="true" MaxLength="15" FieldIndex="16" />
<Field Name="CompanyName" Type="string" MaxLength="40" FieldIndex="14" />
<Field Name="Country" Type="string" IsOptional="true" MaxLength="15" FieldIndex="19" />
<Field Name="CustomerId" Type="string" IsOptional="true" MaxLength="5" FieldIndex="1" />
<Field Name="EmployeeId" Type="int" IsOptional="true" Precision="10" FieldIndex="2" />
<Field Name="Freight" Type="decimal" IsOptional="true" Precision="19" Scale="4" FieldIndex="7" />
<Field Name="OrderDate" Type="datetime" IsOptional="true" FieldIndex="3" />
<Field Name="OrderId" Type="int" Precision="10" FieldIndex="0" />
<Field Name="PostalCode" Type="string" IsOptional="true" MaxLength="10" FieldIndex="18" />
<Field Name="Region" Type="string" IsOptional="true" MaxLength="15" FieldIndex="17" />
<Field Name="RequiredDate" Type="datetime" IsOptional="true" FieldIndex="4" />
<Field Name="ShipAddress" Type="string" IsOptional="true" MaxLength="60" FieldIndex="9" />
<Field Name="ShipCity" Type="string" IsOptional="true" MaxLength="15" FieldIndex="10" />
<Field Name="ShipCountry" Type="string" IsOptional="true" MaxLength="15" FieldIndex="13" />
<Field Name="ShipName" Type="string" IsOptional="true" MaxLength="40" FieldIndex="8" />
<Field Name="ShippedDate" Type="datetime" IsOptional="true" FieldIndex="5" />
<Field Name="ShipPostalCode" Type="string" IsOptional="true" MaxLength="10" FieldIndex="12" />
<Field Name="ShipRegion" Type="string" IsOptional="true" MaxLength="15" FieldIndex="11" />
<Field Name="ShipVia" Type="int" IsOptional="true" Precision="10" FieldIndex="6" />
</Fields>
And the Field Indexes were set, so yeah, now item.Fields will work and FieldIndexes are no longer -1.
So I now realize I probably had a wrong assumption that: "setting the 'Reset order upon refresh' flag on the project BEFORE reverse engineering tables and views into the project would set the field order correctly'. You truly have to run the refresh command, after first reverse-engineering. The name of that setting should have tipped me off.
To validate, I tried on V4 of llblgen as follows:
- created a new project
- set the flag in the designer and saved the project
<Property Name="ResetFieldOrderBasedOnTargetOrderAtRefresh" Value="true" />
- imported the Northwind database and reverse engineered the tables into entities, and views into typed views
- item.Fields returns the fields ordered by name (fieldindex = -1)
- did a refresh
- item.Fields now the order that first expected and field indexes are set
It would be nice IMO if the field order was set correctly after first reverse engineering the tables/views when that field is set, instead of requiring a refresh. Or maybe I'm missing something and this can already be done.
I don't know why this escaped me. At least I know what's going on now, and I'll make sure to refresh all projects consistently from now on.