Skip to content

Settings.OrderProperties

Simon Hughes edited this page Aug 30, 2026 · 2 revisions

Settings.OrderProperties

Puts the generated properties in database column order, or in alphabetical order.

Type OrderProperties enum: Ordinal, Alphabetical
Default OrderProperties.Ordinal
Applies to EF 6 and EF Core
Databases All
In Database.tt? No. You have to add it yourself

What it does

Ordinal keeps the order the columns appear in the table. Alphabetical sorts them by property name.

That is the whole setting. It affects only the order properties are written in - not the mapping, not the column order in the database, not anything EF does.

Example

Product's columns are declared in the order ProductId, ProductName, UnitPrice, Notes, CategoryId, DisplayLabel.

OrderProperties.Ordinal (default)

    // Product
    public class Product
    {
        public int ProductId { get; set; } // ProductId (Primary key)
        public string ProductName { get; set; } // ProductName (length: 100)
        public decimal UnitPrice { get; set; } // UnitPrice
        public string Notes { get; set; } // Notes
        public int CategoryId { get; set; } // CategoryId
        public string DisplayLabel { get; private set; } // DisplayLabel (length: 150)

        // Foreign keys

        /// <summary>
        /// Parent Category pointed by [Product].([CategoryId]) (FK_Product_Category)
        /// </summary>
        public Category Category { get; set; } // FK_Product_Category

        public Product()
        {
            UnitPrice = 0m;
        }
    }

OrderProperties.Alphabetical

    // Product
    public class Product
    {
        public int CategoryId { get; set; } // CategoryId
        public string DisplayLabel { get; private set; } // DisplayLabel (length: 150)
        public string Notes { get; set; } // Notes
        public int ProductId { get; set; } // ProductId (Primary key)
        public string ProductName { get; set; } // ProductName (length: 100)
        public decimal UnitPrice { get; set; } // UnitPrice

        // Foreign keys

        /// <summary>
        /// Parent Category pointed by [Product].([CategoryId]) (FK_Product_Category)
        /// </summary>
        public Category Category { get; set; } // FK_Product_Category

        public Product()
        {
            UnitPrice = 0m;
        }
    }

Note that the navigation properties and the constructor stay where they are. Only the scalar properties are reordered.

When to use it

Ordinal for most people. The generated class reads like the table, which makes comparing the two easy, and a column added at the end of the table appears at the end of the class - so the diff when you regenerate is one added line.

Alphabetical for a wide table you look things up in. Once a table has forty columns, "where is ModifiedOn?" is a real question, and alphabetical answers it. It also makes the class order stable against someone reordering columns in the database.

Gotchas

Not in Database.tt. Add the line yourself:

Settings.OrderProperties = OrderProperties.Alphabetical;

Switching it produces an enormous diff, once. Every property in every entity moves. Do it in a commit of its own, or reviewing the next real change becomes impossible.

It does not affect the primary key. [Key, Column(Order = n)] under Settings.UseDataAnnotations uses the key ordinal from the database, which is independent of this.

Alphabetical sorts on the C# name, not the column name. With Settings.UsePascalCase on, order_date sorts under O for OrderDate. That is almost always what you want, but it means the order will not match ORDER BY column_name in the database.

It has no effect on the configuration class, where builder.Property(...) lines stay in ordinal order regardless.

See also

Clone this wiki locally