-
Notifications
You must be signed in to change notification settings - Fork 226
Settings.UsePropertyInitialisers
Sets column defaults with a property initialiser instead of a constructor, removing the generated constructor entirely.
| Type | bool |
| Default | false |
| Applies to | EF 6 and EF Core |
| Databases | All |
In Database.tt? |
Yes |
When a column has a database default, the generator makes the C# property start with the same value, so a new entity matches what the database would have produced. There are two ways to write that, and this setting picks between them.
false generates a constructor:
public Product()
{
UnitPrice = 0m;
}true generates a C# 6 property initialiser and no constructor at all:
public decimal UnitPrice { get; set; } = 0m;The two behave identically at run time. The difference is where the value lives, and what else the choice drags in.
Product.UnitPrice has DEFAULT ((0)); Category has a reverse navigation collection to initialise.
// 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 ProductId { get; set; } // ProductId (Primary key)
public string ProductName { get; set; } // ProductName (length: 100)
public decimal UnitPrice { get; set; } = 0m; // 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
}The constructor is gone and the default has moved up onto the property.
Turn it on for readability. The default sits next to the property it belongs to rather than several lines away, which is easier to scan on a wide entity.
Turn it on if you want to write your own constructor in a partial class. With the generated constructor gone there is nothing to collide with. With it present, adding a parameterless constructor of your own in a partial file is a compiler error.
Leave it off if you target C# 5 or earlier. Property initialisers are C# 6, which in practice means
anything modern - but old .NET Framework projects pinned to an old language version cannot use them.
Collections still get a constructor. A reverse navigation property is initialised to new List<T>(), and
that happens in a constructor regardless of this setting - look at the Category sample above, which keeps
its constructor either way. This setting only moves scalar defaults.
It has no effect where nothing has a default. An entity with no defaulted columns and no collections generates no constructor either way, so the setting appears to do nothing on some tables and not others.
It does not change what the database does. The initialiser mirrors the database default at the moment the code was generated. Change the default in the database and re-run the generator, or the two drift apart silently.
Settings.IncludeColumnsWithDefaults = false turns the whole thing off. That setting decides whether
defaults are emitted at all; this one only decides how. With defaults suppressed, this setting does nothing.
- Settings.IncludeColumnsWithDefaults - whether defaults are emitted at all
- Settings.GenerateHasDefaultValueSql - putting the default in the EF model instead of the POCO
- Settings.EntityClassesModifiers - making the class partial so you can add your own constructor
- Settings Reference
- 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
- Custom File-Based Templates
- Extra entities via partial classes
- INotifyPropertyChanged
- Syntax colour for T4