Database Order for Fields

Posts   
 
    
cjbiggs
User
Posts: 64
Joined: 17-Apr-2009
# Posted on: 17-Jun-2011 20:12:08   

I am using LLBLGEN 3.1, EF 4.1, and Npgsql 2.0.11.91 (Entity Framework 4 mode)

When I generated code using the LLBLGEN Designer generates the CSDL field names for the Entities in Sorted Order rather than Database Order. I need to have the CSDL field name in Database Order since I used the Microsoft Entity Designer to intially write my code. All my parameters to my code is based on the generated code having the CSDL field names in Database Order. Is there a flag or setting in the Designer that would fix this?

Below is an example for both LLBLGEN Generated CSDL and Microsoft Entity Model Designer Notice the field order for each to see the problem.

LLBLGEN Generated Code

<?xml version="1.0" encoding="utf-8"?> <edmx:Edmx Version="2.0" xmlns:edmx="http://schemas.microsoft.com/ado/2008/10/edmx"> <edmx:Runtime> <edmx:StorageModels> <Schema Namespace="PriceBook.PostgreSql.Store" Alias="Self" Provider="Npgsql" ProviderManifestToken="8.4.1" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl"> <EntityContainer Name="PriceBookPostgreSqlContainer"> <EntitySet Name="setup_item" EntityType="Self.setup_item" Schema="public" /> <!-- __LLBLGENPRO_USER_CODE_REGION_START SSDLEntityContainerCustomCode --> <!-- __LLBLGENPRO_USER_CODE_REGION_END --> </EntityContainer> <EntityType Name="setup_item"> <Key> <PropertyRef Name="item_id"/> <PropertyRef Name="setup_id"/> </Key> <Property Name="item_id" Type="int4" Nullable="false" /> <Property Name="setup_id" Type="int4" Nullable="false" /> <Property Name="added_audit_id" Type="int4" /> <Property Name="last_modified_audit_id" Type="int4" /> <Property Name="item_status_name" Type="varchar" MaxLength="30" /> <Property Name="item_state_name" Type="varchar" MaxLength="50" /> <Property Name="item_action_name" Type="varchar" MaxLength="50" /> <Property Name="source_setup_id" Type="int4" /> <Property Name="is_in_master" Type="bool" /> <Property Name="active" Type="bool" /> <Property Name="department" Type="int4" /> <Property Name="subdepartment" Type="int4" /> <Property Name="upc_pack_size" Type="int4" /> <Property Name="can_buy" Type="bool" /> <Property Name="can_sell" Type="bool" /> <Property Name="description" Type="varchar" MaxLength="100" /> <Property Name="redemption_number" Type="int4" /> <Property Name="item_type_name" Type="varchar" MaxLength="100" /> <Property Name="tax_group_name" Type="varchar" MaxLength="100" /> </EntityType> <!-- __LLBLGENPRO_USER_CODE_REGION_START SSDLCustomCode --> <!-- __LLBLGENPRO_USER_CODE_REGION_END --> </Schema> </edmx:StorageModels> <edmx:ConceptualModels> <Schema Namespace="PriceBookModel" Alias="Self" xmlns="http://schemas.microsoft.com/ado/2008/09/edm"> <EntityContainer Name="PriceBookEntities"> <EntitySet Name="SetupItem" EntityType="Self.SetupItem"/> <!-- __LLBLGENPRO_USER_CODE_REGION_START CSDLEntityContainerCustomCode --> <!-- __LLBLGENPRO_USER_CODE_REGION_END --> </EntityContainer> <EntityType Name="SetupItem"> <Key> <PropertyRef Name="ItemId"/> <PropertyRef Name="SetupId"/> </Key> <Property Name="Active" Type="Boolean" /> <Property Name="AddedAuditId" Type="Int32" /> <Property Name="CanBuy" Type="Boolean" /> <Property Name="CanSell" Type="Boolean" /> <Property Name="Department" Type="Int32" /> <Property Name="Description" Type="String" MaxLength="100" FixedLength="false" Unicode="true" /> <Property Name="IsInMaster" Type="Boolean" /> <Property Name="ItemActionName" Type="String" MaxLength="50" FixedLength="false" Unicode="true" /> <Property Name="ItemId" Nullable="false" Type="Int32" /> <Property Name="ItemStateName" Type="String" MaxLength="50" FixedLength="false" Unicode="true" /> <Property Name="ItemStatusName" Type="String" MaxLength="30" FixedLength="false" Unicode="true" /> <Property Name="ItemTypeName" Type="String" MaxLength="100" FixedLength="false" Unicode="true" /> <Property Name="LastModifiedAuditId" Type="Int32" /> <Property Name="RedemptionNumber" Type="Int32" /> <Property Name="SetupId" Nullable="false" Type="Int32" /> <Property Name="SourceSetupId" Type="Int32" /> <Property Name="Subdepartment" Type="Int32" /> <Property Name="TaxGroupName" Type="String" MaxLength="100" FixedLength="false" Unicode="true" /> <Property Name="UpcPackSize" Type="Int32" /> </EntityType> <!-- __LLBLGENPRO_USER_CODE_REGION_START CSDLCustomCode --> <!-- __LLBLGENPRO_USER_CODE_REGION_END --> </Schema> </edmx:ConceptualModels> <edmx:Mappings> <Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2008/09/mapping/cs"> <EntityContainerMapping StorageEntityContainer="PriceBookPostgreSqlContainer" CdmEntityContainer="PriceBookEntities"> <EntitySetMapping Name="SetupItem"> <EntityTypeMapping TypeName="IsTypeOf(PriceBookModel.SetupItem)"> <MappingFragment StoreEntitySet="setup_item"> <ScalarProperty Name="ItemId" ColumnName="item_id" /> <ScalarProperty Name="SetupId" ColumnName="setup_id" /> <ScalarProperty Name="AddedAuditId" ColumnName="added_audit_id" /> <ScalarProperty Name="LastModifiedAuditId" ColumnName="last_modified_audit_id" /> <ScalarProperty Name="ItemStatusName" ColumnName="item_status_name" /> <ScalarProperty Name="ItemStateName" ColumnName="item_state_name" /> <ScalarProperty Name="ItemActionName" ColumnName="item_action_name" /> <ScalarProperty Name="SourceSetupId" ColumnName="source_setup_id" /> <ScalarProperty Name="IsInMaster" ColumnName="is_in_master" /> <ScalarProperty Name="Active" ColumnName="active" /> <ScalarProperty Name="Department" ColumnName="department" /> <ScalarProperty Name="Subdepartment" ColumnName="subdepartment" /> <ScalarProperty Name="UpcPackSize" ColumnName="upc_pack_size" /> <ScalarProperty Name="CanBuy" ColumnName="can_buy" /> <ScalarProperty Name="CanSell" ColumnName="can_sell" /> <ScalarProperty Name="Description" ColumnName="description" /> <ScalarProperty Name="RedemptionNumber" ColumnName="redemption_number" /> <ScalarProperty Name="ItemTypeName" ColumnName="item_type_name" /> <ScalarProperty Name="TaxGroupName" ColumnName="tax_group_name" /> </MappingFragment> </EntityTypeMapping> </EntitySetMapping> <!-- __LLBLGENPRO_USER_CODE_REGION_START MSLEntityContainerMappingCustomCode --> <!-- __LLBLGENPRO_USER_CODE_REGION_END --> </EntityContainerMapping> <!-- __LLBLGENPRO_USER_CODE_REGION_START MSLCustomCode --> <!-- __LLBLGENPRO_USER_CODE_REGION_END --> </Mapping> </edmx:Mappings> </edmx:Runtime> <!-- Designer element for auto-embedding the .edmx file into the compiled assembly as separated resource files and to validate it on build. --> <edmxsmile esigner xmlns="http://schemas.microsoft.com/ado/2008/10/edmx"> <edmx:Connection> <DesignerInfoPropertySet> <DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" /> </DesignerInfoPropertySet> </edmx:Connection> <edmxfrowning ptions> <DesignerInfoPropertySet> <DesignerProperty Name="ValidateOnBuild" Value="true" /> </DesignerInfoPropertySet> </edmxfrowning ptions> <!-- __LLBLGENPRO_USER_CODE_REGION_START DesignerCustomCode --> <!-- __LLBLGENPRO_USER_CODE_REGION_END --> </edmxsmile esigner> </edmx:Edmx>

Microsoft Entity Model Designer using Devart Art Provider

<?xml version="1.0" encoding="utf-8"?> <edmx:Edmx Version="2.0" xmlns:edmx="http://schemas.microsoft.com/ado/2008/10/edmx">

<!-- EF Runtime content -->

<edmx:Runtime> <!-- SSDL content --> <edmx:StorageModels> <Schema Namespace="LoadPriceBookModel.Store" Alias="Self" Provider="Devart.Data.PostgreSql" ProviderManifestToken="PostgreSQL" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl"> <EntityContainer Name="LoadPriceBookModelStoreContainer"> <EntitySet Name="setup_item" EntityType="LoadPriceBookModel.Store.setup_item" store:Type="Tables" Schema="public" /> </EntityContainer> <EntityType Name="setup_item"> <Key> <PropertyRef Name="item_id" /> <PropertyRef Name="setup_id" /> </Key> <Property Name="item_id" Type="int" Nullable="false" /> <Property Name="setup_id" Type="int" Nullable="false" /> <Property Name="added_audit_id" Type="int" /> <Property Name="last_modified_audit_id" Type="int" /> <Property Name="item_status_name" Type="varchar" MaxLength="30" /> <Property Name="item_state_name" Type="varchar" MaxLength="50" /> <Property Name="item_action_name" Type="varchar" MaxLength="50" /> <Property Name="source_setup_id" Type="int" /> <Property Name="is_in_master" Type="boolean" /> <Property Name="active" Type="boolean" /> <Property Name="department" Type="int" /> <Property Name="subdepartment" Type="int" /> <Property Name="upc_pack_size" Type="int" /> <Property Name="can_buy" Type="boolean" /> <Property Name="can_sell" Type="boolean" /> <Property Name="description" Type="varchar" MaxLength="100" /> <Property Name="redemption_number" Type="int" /> <Property Name="item_type_name" Type="varchar" MaxLength="100" /> <Property Name="tax_group_name" Type="varchar" MaxLength="100" /> </EntityType> </Schema> </edmx:StorageModels> <!-- CSDL content --> <edmx:ConceptualModels> <Schema Namespace="LoadPriceBookModel" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2008/09/edm"> <EntityContainer Name="LoadPriceBookEntities" annotation:LazyLoadingEnabled="true"> <EntitySet Name="setup_item" EntityType="LoadPriceBookModel.setup_item" /> </EntityContainer> <EntityType Name="setup_item"> <Key> <PropertyRef Name="item_id" /> <PropertyRef Name="setup_id" /> </Key> <Property Name="item_id" Type="Int32" Nullable="false" /> <Property Name="setup_id" Type="Int32" Nullable="false" /> <Property Name="added_audit_id" Type="Int32" /> <Property Name="last_modified_audit_id" Type="Int32" /> <Property Name="item_status_name" Type="String" MaxLength="30" Unicode="true" FixedLength="false" /> <Property Name="item_state_name" Type="String" MaxLength="50" Unicode="true" FixedLength="false" /> <Property Name="item_action_name" Type="String" MaxLength="50" Unicode="true" FixedLength="false" /> <Property Name="source_setup_id" Type="Int32" /> <Property Name="is_in_master" Type="Boolean" /> <Property Name="active" Type="Boolean" /> <Property Name="department" Type="Int32" /> <Property Name="subdepartment" Type="Int32" /> <Property Name="upc_pack_size" Type="Int32" /> <Property Name="can_buy" Type="Boolean" /> <Property Name="can_sell" Type="Boolean" /> <Property Name="description" Type="String" MaxLength="100" Unicode="true" FixedLength="false" /> <Property Name="redemption_number" Type="Int32" /> <Property Name="item_type_name" Type="String" MaxLength="100" Unicode="true" FixedLength="false" /> <Property Name="tax_group_name" Type="String" MaxLength="100" Unicode="true" FixedLength="false" /> </EntityType> </Schema> </edmx:ConceptualModels> <!-- C-S mapping content --> <edmx:Mappings> <Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2008/09/mapping/cs"> <EntityContainerMapping StorageEntityContainer="LoadPriceBookModelStoreContainer" CdmEntityContainer="LoadPriceBookEntities"> <EntitySetMapping Name="setup_item"><EntityTypeMapping TypeName="LoadPriceBookModel.setup_item"><MappingFragment StoreEntitySet="setup_item"> <ScalarProperty Name="item_id" ColumnName="item_id" /> <ScalarProperty Name="setup_id" ColumnName="setup_id" /> <ScalarProperty Name="added_audit_id" ColumnName="added_audit_id" /> <ScalarProperty Name="last_modified_audit_id" ColumnName="last_modified_audit_id" /> <ScalarProperty Name="item_status_name" ColumnName="item_status_name" /> <ScalarProperty Name="item_state_name" ColumnName="item_state_name" /> <ScalarProperty Name="item_action_name" ColumnName="item_action_name" /> <ScalarProperty Name="source_setup_id" ColumnName="source_setup_id" /> <ScalarProperty Name="is_in_master" ColumnName="is_in_master" /> <ScalarProperty Name="active" ColumnName="active" /> <ScalarProperty Name="department" ColumnName="department" /> <ScalarProperty Name="subdepartment" ColumnName="subdepartment" /> <ScalarProperty Name="upc_pack_size" ColumnName="upc_pack_size" /> <ScalarProperty Name="can_buy" ColumnName="can_buy" /> <ScalarProperty Name="can_sell" ColumnName="can_sell" /> <ScalarProperty Name="description" ColumnName="description" /> <ScalarProperty Name="redemption_number" ColumnName="redemption_number" /> <ScalarProperty Name="item_type_name" ColumnName="item_type_name" /> <ScalarProperty Name="tax_group_name" ColumnName="tax_group_name" /> </MappingFragment></EntityTypeMapping></EntitySetMapping> </EntityContainerMapping> </Mapping> </edmx:Mappings> </edmx:Runtime>

<!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->

<Designer xmlns="http://schemas.microsoft.com/ado/2008/10/edmx"> <Connection> <DesignerInfoPropertySet> <DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" /> </DesignerInfoPropertySet> </Connection> <Options> <DesignerInfoPropertySet> <DesignerProperty Name="ValidateOnBuild" Value="true" /> <DesignerProperty Name="EnablePluralization" Value="True" /> <DesignerProperty Name="IncludeForeignKeysInModel" Value="True" /> </DesignerInfoPropertySet> </Options> <!-- Diagram content (shape and connector positions) --> <Diagrams> <Diagram Name="Model1"> <EntityTypeShape EntityType="LoadPriceBookModel.setup_item" Width="1.5" PointX="0.75" PointY="0.75" Height="4.6725065104166656" IsExpanded="true" /> </Diagram> </Diagrams> </Designer> </edmx:Edmx>

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 17-Jun-2011 22:54:57   

Since v3 the field order is arbitrary, this is because the order of the DB could change as well. So to avoid this the alphabetically order was used, not the DB order. If you want more details about the reason, please read this: http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=18277&StartAtMessage=0&#102335

In v3.1 there is a feature which help you to setup a user custom field order. If you edit an entity you can click the "Set Field Order" button. This way you can setup your own order. You have to do this in every entity you want to change the field order.

Anyway, the fact that the fields at EDMX is generated in alphabetical order shouldn't affect your code. If it does, please explain us why.

David Elizondo | LLBLGen Support Team
cjbiggs
User
Posts: 64
Joined: 17-Apr-2009
# Posted on: 18-Jun-2011 01:58:02   

The code/object generated from the CSDL no longer matches existing code I wrote when I used the Microsoft Entity Model Designer. The parameters to some methods are in different orders now. So I would have to go through all the code and fix the order since they are not named parameters.

Why not have an option to maintain the database order for cases where the order doesnt need to change?

Thanks,

Charlie J.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 19-Jun-2011 21:26:57   

cjbiggs wrote:

Why not have an option to maintain the database order for cases where the order doesnt need to change?

There is an option (I missed it): set **ResetFieldOrderBasedOnTargetOrderAtRefresh **and **UseCustomFieldOrderingOnNewElements **to true at Project Properties, then do a Catalog Refresh.

For more info read the docs about this.

David Elizondo | LLBLGen Support Team
cjbiggs
User
Posts: 64
Joined: 17-Apr-2009
# Posted on: 20-Jun-2011 01:29:22   

Thanks Daelmo. I had already tried that and it still sorted the order in the CSDL. The SSDL and Mapping Layer are in Database Order and the CSDL is in sorted order by Field Name. I am using the later build for LLBLGEN 3.1 released on June 17, 2011. Can I modifed this behaviour by changing the LLBLGEN Templates or a Custom T4 Template?

Thanks,

Charlie J.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 20-Jun-2011 10:06:55   

In v3.1 we don't order the fields in the order of the project for EF / NHibernate / Linq to SQL as the order isn't used anywhere in these frameworks.

It seems that you run into an edge case though, as you have started with a different designer.... it is possible to order the fields on the ordering. There's an extension method available in ApplicationCore: ApplyFieldOrdering. It can be applied as:

var fieldsToTraverse = entity.Fields.Where(f=>!f.IsDiscriminator).ApplyFieldOrdering().ToList();

(line 305 in EDMX Include). Ordering has impact on pk / fk field ordering! so for compound PK fields, it might be code changes in other templates as well (like the dreadful STE / POCO templates). We haven't checked this. Everywhere where .OrderBy(f=>f.Name) or similar is present on fields, this can be changed by ApplyFieldOrdering().

cjbiggs
User
Posts: 64
Joined: 17-Apr-2009
# Posted on: 20-Jun-2011 17:40:11   

So I would have to edit the EDMX file to make the change or edit one of the templates?

Thanks,

Charlie J.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 21-Jun-2011 05:02:35   

You would have to edit the EDMX include template. It's located at: Frameworks\Entity Framework\Templates\Shared\Shared. The line is 297. An example of the modified template is attached.

var fieldsToTraverse = entity.Fields.Where(f=>!f.IsDiscriminator).ApplyFieldOrdering().ToList();

instead of

var fieldsToTraverse = entity.Fields.Where(f=>!f.IsDiscriminator).OrderBy(f => f.Name).ToList();
Attachments
Filename File size Added on Approval
edmxFileInclude.lpt 35,713 21-Jun-2011 05:03.05 Approved
David Elizondo | LLBLGen Support Team
cjbiggs
User
Posts: 64
Joined: 17-Apr-2009
# Posted on: 21-Jun-2011 15:26:32   

Thanks David. I had already figured that out and was able to get it to work for my Edge Case. Can we add this as an option in the Preferences and Properties, because some might have coded there application using other designers which uses Database Order?

Thanks,

Charlie J.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39908
Joined: 17-Aug-2003
# Posted on: 21-Jun-2011 16:20:00   

cjbiggs wrote:

Thanks David. I had already figured that out and was able to get it to work for my Edge Case. Can we add this as an option in the Preferences and Properties, because some might have coded there application using other designers which uses Database Order?

Thanks,

Charlie J.

Added to todo list. Not certain this will make the cut for the next version (as those features are already scheduled and prioritized) but we'll see what we can do simple_smile

Frans Bouma | Lead developer LLBLGen Pro
cjbiggs
User
Posts: 64
Joined: 17-Apr-2009
# Posted on: 22-Jun-2011 15:45:04   

Great Thanks Otis as always and everyone for there help on this matter.

Thanks,

Charlie J.