🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
DefaultDBValue
This page was created by Lars.olofsson on 2024-01-16. Last edited by Wikiadmin on 2026-07-29.

DefaultDBValue lets you provide the database value used when MDriven evolves or creates a database column, primarily when you add a non-nullable attribute to a table that already contains rows.

When to use DefaultDBValue

Set DefaultDBValue in the property inspector for a modelled attribute when all of the following apply:

  • The attribute is persisted to the database.
  • Allow Null is set to false.
  • You are creating or evolving a database where the corresponding table may already contain rows.

When MDriven adds a new non-nullable column, the database must assign a value to every existing row. Without a valid DefaultDBValue, those rows would receive no value even though the new column does not allow nulls, and the evolve operation fails.

If Allow Null is true, a DefaultDBValue is not needed because existing rows can hold no value for the new column.

Enter a database value, not an in-memory value

DefaultDBValue must use the format expected by the database engine. For SQL Server, this differs from the value you enter in InitialValue, which is used when MDriven creates an object in memory.

Attribute type and nullability DefaultDBValue for SQL Server Corresponding InitialValue example
Non-nullable Boolean 0 or 1 false or true
Non-nullable Decimal 0 or another numeric value 0 or another numeric value
String 'stringvalue' stringvalue

For a Boolean, use 1 for true and 0 for false. MDrivenServer persistence mapping represents Boolean values as a database bit; see Documentation:Case sensitive or not.

For a string DefaultDBValue, include the database quotes. Do not include those quotes in InitialValue.

Add a non-nullable attribute safely

Use this sequence when adding a persisted, non-nullable attribute to an existing model:

  1. Add the attribute in MDriven Designer.
  2. In the property inspector, set Allow Null to false.
  3. Enter a valid database literal in DefaultDBValue. For example, set 0 for a new non-nullable Decimal attribute, or 1 for a Boolean attribute that must be true for existing rows.
  4. Evolve the database.
  5. Set InitialValue as well if newly created in-memory objects need an initial value before they are stored.

For example, if you add a non-nullable Boolean attribute named IsArchived to a class whose table already has rows, set DefaultDBValue to 0. During evolution, existing rows receive the database value 0.

Empty tables and later changes

An evolve may succeed without DefaultDBValue when the affected table is empty. This does not make the setting optional for a non-nullable attribute: after the column exists, you cannot add the required creation-time setting without dropping the attribute and adding it again.

If you evolved an attribute with the wrong nullability and do not need the existing data in that field, remove the database field and add it again with the intended settings. One way to cause this through evolution is to rename the attribute, evolve so the original field is dropped, then rename it back and evolve again after setting the correct DefaultDBValue.

DefaultDBValue, InitialValue, and Allow Null

These properties address different stages of an attribute's life:

Property Controls Example for a Boolean attribute
Allow Null Whether the value may be absent in memory and in the database false requires a value
InitialValue The value used when a new object is created in memory false
DefaultDBValue The database-formatted value used when the database is created or evolved 0

Read Documentation:Initial values and Default Database values for the complete explanation of how these properties work together.

See also

[[Category:Attributes and data types [Beginner]]]