54

Is it possible using data annotations to add default value for int property

something like

[DefaultValue=1]
public int MyId {get; set;}
user1765862
  • 13,635
  • 28
  • 115
  • 220
  • 4
    It doesn't work... Just use the constructor. – Andrei V May 23 '14 at 07:12
  • 1
    possible duplicate [Annotating properties on a model with default values](http://stackoverflow.com/questions/6779881/annotating-properties-on-a-model-with-default-values) – scheien May 23 '14 at 07:13

5 Answers5

77

Try this - set the default value in the constructor:

public class YOURMODEL
{
    public int MyId { get; set; }  

    public YOURMODEL()
    { 
        MyId = 1;       
    }
}

Later addition by other user: Since C# 6.0 (2015) this simpler syntax has been allowed:

public class YOURMODEL
{
    public int MyId { get; set; } = 1;
}
Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
Nilesh Gajare
  • 6,302
  • 3
  • 42
  • 73
16

Use [DefaultValue(false)].

(Reference)

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
bellona
  • 161
  • 1
  • 2
  • No, DefaultValueAttribute is not enough. You must also set the value by code in constructor. See https://learn.microsoft.com/en-us/troubleshoot/developer/dotnet/framework/general/confusing-defaultvalueattribute – Canada Wan Jan 24 '22 at 19:17
2

The constructor method is correct of course (as per @Nilesh) but this solution doesn't address any legacy data you might have already created in your database.

You can also update your legacy data by generating the migration and then adjusting the AddColumn method thusly...

AddColumn("dbo.Orgs", "MyId", c => c.Int(nullable: false));

changes to:

AddColumn("dbo.Orgs", "MyId", c => c.Int(nullable: false, defaultValue: 1));

Note, this will also create a database trigger that would automatically update the default value on INSERT so you don't technically need the constructor method from the database perspective but setting the value using the constructor is still the best practice.

spadelives
  • 1,588
  • 13
  • 23
1

You can only do this using the class' constructor. Your code should thus look like this:

public class MyModel
{
    public MyModel()
    {
        MyId = 1;
    }

    public int MyId {get; set;}
}

This will lead to the MyId property being set to 1 whenever a new instance of the class is made. However, if model binding detects that the user has specified a value for MyId, it will overwrite the default value with the user-specified one.

Erik Schierboom
  • 16,301
  • 10
  • 64
  • 81
-1
using System.ComponentModel;

namespace EMS.Models
{
    public class Admission
    {
        [DefaultValue(false)]
        public Boolean LCStatus { get; set; }

        [DefaultValue("India")]
        public string Country{ get; set; }
    }
}
Pankaj Lahoti
  • 2,792
  • 1
  • 7
  • 8
  • Although this code might solve the problem, a good answer should explain **what** the code does and **how** it helps. – BDL May 19 '20 at 15:49
  • You can learn more on DefaultValue **Attribute** from this [link] (https://www.codeproject.com/Articles/66073/DefaultValue-Attribute-Based-Approach-to-Property) – Pankaj Lahoti May 19 '20 at 16:41