-
Notifications
You must be signed in to change notification settings - Fork 226
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 |
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.
Product's columns are declared in the order ProductId, ProductName, UnitPrice, Notes, CategoryId,
DisplayLabel.
// 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;
}
} // 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.
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.
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.
- Settings.UsePascalCase - which decides the names being sorted
- Settings.UseDataAnnotations - key ordering, which this does not touch
- Settings Reference
- Home
- Compared with the Microsoft scaffolder
- Connection strings
- JetBrains Rider
- Upgrading from v3 to v4
- Saving .tt does nothing
- Settings A-Z - every setting, with a page each
- Common Settings Types Explained
- Settings Callbacks
- Settings runtime values and helpers
- Filtering
- Full Control Over the Generated Code
- Enum Generation from Table Data
- Owned Entities
- JSON column support
- Global Query Filters
- Extended Property Names Feature
- Partial Properties
- File-Scoped Namespaces
- Data Annotations
- Spatial Types
- HierarchyId
- RowVersion and TimeStamp columns
- Lazy Loading
- Stored proc result sets