Skip to content

Settings.UsePropertyInitialisers

Simon Hughes edited this page Aug 30, 2026 · 1 revision

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

What it does

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.

Example

Product.UnitPrice has DEFAULT ((0)); Category has a reverse navigation collection to initialise.

Settings.UsePropertyInitialisers = false (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;
        }
    }

Settings.UsePropertyInitialisers = true

    // 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.

When to use it

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.

Gotchas

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.

See also

Clone this wiki locally