264

is there "elegant" way to give specific property a default value ?

Maybe by DataAnnotations, something like :

[DefaultValue("true")]
public bool Active { get; set; }

Thank you.

marino-krk
  • 2,651
  • 3
  • 13
  • 5
  • 1
    Maybe try in the constructor `this.Active = true;`? I think the DB value will take precedence when fetching, but careful if new'ing then attaching an entity for an update without a fetch first, as the change tracking *might* see this as you wanting to update the value. Comment because I haven't used EF in a long time, and I feel like this is a shot in the dark. – AaronLS Oct 23 '13 at 23:22
  • 3
    Thank you for response, I have used this method so far http://stackoverflow.com/a/5032578/2913441 but I thought that maybe there is a better way. – marino-krk Oct 23 '13 at 23:34
  • 4
    `public bool Inactive { get; set; }` – Jesse Hufstetler Feb 23 '18 at 19:02
  • as Microsoft docs say "You can not set a default value using Data Annotations." – hmfarimani Dec 23 '18 at 09:23
  • Please refere [https://stackoverflow.com/a/59551802/8403632](https://stackoverflow.com/a/59551802/8403632) – shalitha senanayaka Jan 01 '20 at 11:06
  • If someone is stumbling across this question looking for an `EF Core` solution to use the `DefaultValue` attribute: https://stackoverflow.com/a/64803061/1462234 – Daniel Z. Nov 12 '20 at 11:37
  • There is a vary simple solution. Please check the answer I left below. – Lakshitha Kanchana Nov 29 '22 at 09:57

18 Answers18

195

You can do it by manually edit code first migration:

public override void Up()
{    
   AddColumn("dbo.Events", "Active", c => c.Boolean(nullable: false, defaultValue: true));
} 
gdbdable
  • 4,445
  • 3
  • 30
  • 46
  • 8
    I'm not sure this will work if OP doesn't specially set `Active` to `true` when creating an `Event` object as well. The default value always be `false` on a non nullable bool property, so unless changed, this is what entity framework will save to the db. Or am I missing something? – GFoley83 Feb 17 '15 at 19:10
  • 8
    @GFoley83, yes, you are right. This method only add default constraint at database level. For complete solution you also need to assign default value in constructor of entity or use property with backed field as shown in answer above – gdbdable Feb 18 '15 at 12:39
  • 1
    This works for base types. For something like DATETIMEOFFSET, use the , defaultValueSql: "SYSDATETIMEOFFSET", and NOT the defaultValue as this defaultValue: System.DateTimeOffset.Now, will resolve to a string of the current system datetimeoffset value. – OzBob Apr 15 '16 at 06:53
  • As I was always using Automatic Migrations Enabled, I did not know how to add migration and where to put your code, for people like me :) this link might be helpful: https://msdn.microsoft.com/en-us/library/jj591621(v=vs.113).aspx Look at Data Motion / Custom SQL part of article. – Dovlet Mamenov Nov 19 '16 at 12:11
  • I noticed some people downvoted. so because this answer is old, before use this you should try to check more recent solutions of problem. Please argue the downvote if is has a reason – gdbdable Jan 24 '18 at 10:41
  • 3
    AFAIK your manual changes will get lost in case of re-scaffolding the migration – Alexander Powolozki May 15 '18 at 06:16
  • I did this solution long ago, but now I want to remove default value, how can I achieve that? Since this solution is only at database level, removing it directly from the database may break EF internals? – ninbit May 27 '20 at 11:41
  • 1
    @ninbit i think you shuld write migration to remove it at database level first, then change your DAL mapping – gdbdable Jun 16 '20 at 07:08
98

It's been a while, but leaving a note for others. I achieved what is needed with an attribute and I decorated my model class fields with that attribute as I want.

[SqlDefaultValue(DefaultValue = "getutcdate()")]
public DateTime CreatedDateUtc { get; set; }

Got the help of these 2 articles:

What I did:

Define Attribute

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class SqlDefaultValueAttribute : Attribute
{
    public string DefaultValue { get; set; }
}

In the "OnModelCreating" of the context

modelBuilder.Conventions.Add( new AttributeToColumnAnnotationConvention<SqlDefaultValueAttribute, string>("SqlDefaultValue", (p, attributes) => attributes.Single().DefaultValue));

In the custom SqlGenerator

private void SetAnnotatedColumn(ColumnModel col)
{
    AnnotationValues values;
    if (col.Annotations.TryGetValue("SqlDefaultValue", out values))
    {
         col.DefaultValueSql = (string)values.NewValue;
    }
}

Then in the Migration Configuration constructor, register the custom SQL generator.

SetSqlGenerator("System.Data.SqlClient", new CustomMigrationSqlGenerator());
ravinsp
  • 4,150
  • 2
  • 32
  • 43
  • 6
    You can even do it globally without putting `[SqlDefaultValue(DefaultValue = "getutcdate()")]` on every entity. 1) Simply remove `modelBuilder.Conventions.Add( new AttributeToColumnAnnotationConvention("SqlDefaultValue", (p, attributes) => attributes.Single().DefaultValue));` 2) Add `modelBuilder.Properties().Where(x => x.PropertyType == typeof(DateTime)).Configure(c => c.HasColumnType("datetime2").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed).HasColumnAnnotation("SqlDefaultValue", "getdate()"));` – Daniel Skowroński Mar 12 '16 at 16:29
  • 2
    Where's the custom SqlGenerator please? – ᴍᴀᴛᴛ ʙᴀᴋᴇʀ Jan 24 '18 at 15:50
  • 3
    Custom SqlGenerator came from here: https://andy.mehalick.com/2014/02/06/ef6-adding-a-created-datetime-column-automatically-with-code-first-migrations/ – ravinsp Jan 25 '18 at 11:03
  • 1
    @ravinsp why don't customize the MigrationCodeGenerator class to have the migration with the right information rather than the SqlGenerator code? The SQL code is the last step... – Alex Mar 20 '18 at 09:00
  • @Alex Worth trying! That would also work and would be more elegant than injecting SQL code. But I'm not sure about the complexity of overriding the C# MigrationCodeGenerator. – ravinsp Mar 22 '18 at 05:11
  • Where is HarmonyMigrationSqlGenerator? – bjnr Sep 19 '18 at 06:58
  • How do you do this in core? – Demodave Mar 07 '19 at 14:02
  • Please correct me if I'm wrong - I think when EF does an insert, whatever value is set in the model object property will then overwrite that default value. Which renders setting any default value in the DB useless. – David Thielen Mar 11 '23 at 19:50
94

The above answers really helped, but only delivered part of the solution. The major issue is that as soon as you remove the Default value attribute, the constraint on the column in database won't be removed. So previous default value will still stay in the database.

Here is a full solution to the problem, including removal of SQL constraints on attribute removal. I am also re-using .NET Framework's native DefaultValue attribute.

Usage

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
[DefaultValue("getutcdate()")]
public DateTime CreatedOn { get; set; }

For this to work you need to update IdentityModels.cs and Configuration.cs files

IdentityModels.cs file

Add/update this method in your ApplicationDbContext class

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
            base.OnModelCreating(modelBuilder);
            var convention = new AttributeToColumnAnnotationConvention<DefaultValueAttribute, string>("SqlDefaultValue", (p, attributes) => attributes.SingleOrDefault().Value.ToString());
            modelBuilder.Conventions.Add(convention);
}

Configuration.cs file

Update your Configuration class constructor by registering custom Sql generator, like this:

internal sealed class Configuration : DbMigrationsConfiguration<ApplicationDbContext>
{
    public Configuration()
    {
        // DefaultValue Sql Generator
        SetSqlGenerator("System.Data.SqlClient", new DefaultValueSqlServerMigrationSqlGenerator());
    }
}

Next, add custom Sql generator class (you can add it to the Configuration.cs file or a separate file)

internal class DefaultValueSqlServerMigrationSqlGenerator : SqlServerMigrationSqlGenerator
{
    private int dropConstraintCount;

    protected override void Generate(AddColumnOperation addColumnOperation)
    {
        SetAnnotatedColumn(addColumnOperation.Column, addColumnOperation.Table);
        base.Generate(addColumnOperation);
    }

    protected override void Generate(AlterColumnOperation alterColumnOperation)
    {
        SetAnnotatedColumn(alterColumnOperation.Column, alterColumnOperation.Table);
        base.Generate(alterColumnOperation);
    }

    protected override void Generate(CreateTableOperation createTableOperation)
    {
        SetAnnotatedColumns(createTableOperation.Columns, createTableOperation.Name);
        base.Generate(createTableOperation);
    }

    protected override void Generate(AlterTableOperation alterTableOperation)
    {
        SetAnnotatedColumns(alterTableOperation.Columns, alterTableOperation.Name);
        base.Generate(alterTableOperation);
    }

    private void SetAnnotatedColumn(ColumnModel column, string tableName)
    {
        if (column.Annotations.TryGetValue("SqlDefaultValue", out var values))
        {
            if (values.NewValue == null)
            {
                column.DefaultValueSql = null;
                using var writer = Writer();

                // Drop Constraint
                writer.WriteLine(GetSqlDropConstraintQuery(tableName, column.Name));
                Statement(writer);
            }
            else
            {
                column.DefaultValueSql = (string)values.NewValue;
            }
        }
    }

    private void SetAnnotatedColumns(IEnumerable<ColumnModel> columns, string tableName)
    {
        foreach (var column in columns)
        {
            SetAnnotatedColumn(column, tableName);
        }
    }

    private string GetSqlDropConstraintQuery(string tableName, string columnName)
    {
        var tableNameSplitByDot = tableName.Split('.');
        var tableSchema = tableNameSplitByDot[0];
        var tablePureName = tableNameSplitByDot[1];

        var str = $@"DECLARE @var{dropConstraintCount} nvarchar(128)
SELECT @var{dropConstraintCount} = name
FROM sys.default_constraints
WHERE parent_object_id = object_id(N'{tableSchema}.[{tablePureName}]')
AND col_name(parent_object_id, parent_column_id) = '{columnName}';
IF @var{dropConstraintCount} IS NOT NULL
EXECUTE('ALTER TABLE {tableSchema}.[{tablePureName}] DROP CONSTRAINT [' + @var{dropConstraintCount} + ']')";

        dropConstraintCount++;
        return str;
    }
}
Rudey
  • 4,717
  • 4
  • 42
  • 84
J-M
  • 1,207
  • 11
  • 18
  • 2
    This approach worked perfectly for me. One enhancement I made was to also override Generate(CreateTableOperation createTableOperation) and Generate(AddColumnOperation addColumnOperation) with the same logic so those scenarios are also caught. I also only check values.NewValue is null as I wanted my default to be an empty string. – Delorian Jul 08 '16 at 04:43
  • 2
    @Delorian I have updated my answer, thank you for your comments – J-M Jul 08 '16 at 07:35
  • 1
    I've made an edit to your post to support instances where a rollback is dropping more than one constraint. Your script would throw an error stating that @con was already declared. I created a private variable to hold a counter and simply increment it. I also changed the format of the drop constraint to more closely match what EF sends to SQL when creating the constraint. Great work on this! – Brad Aug 25 '16 at 02:58
  • I found another issue but I'm not sure how to resolve it. If you change the datatype, the alter column fails because of the default value constraint. You have to remove the default, when you change the column type then re-add the default in a separate migration. – Brad Aug 25 '16 at 15:11
  • When I implement this, two things: First, it doesn't just create a `c.String(maxLength: 1, defaultValue: "A")`, like I expected, it creates an entire annotations dictionary. But worse, when that generates SQL it generates `[DeleteCode] [nvarchar](1) DEFAULT A`, instead of `DEFAULT ('A')` and then errors on execution. – IronSean Feb 28 '17 at 19:28
  • 1
    Thanks for solution but I have two problems: 1. Table names need brackets. 2. In update new value not set and default value set instead! – Omid.Hanjani Apr 15 '17 at 11:53
  • @Omid-RH Thanks for your edit. Could you please give more details on issue #2 – J-M Apr 15 '17 at 17:26
  • @J_M You're Welcome, I have a property with [DefaultValue(1)], When I set new value (for example 3) to my property and update my entity, my field has default value (is 1 not 3) – Omid.Hanjani Apr 16 '17 at 06:56
  • 1
    Do I need the set the `[DatabaseGenerated(DatabaseGeneratedOption.Computed)]` attribute? If so, why? In my tests it seemed to have no effect leaving it out. – RamNow Jul 17 '18 at 13:10
  • Moreover: The generated Down()-part in the migration doesn't simply drop tables, it looks like this: `DropTable("dbo.PERSON", removedColumnAnnotations: new Dictionary> { { "DATEOFBITH", new Dictionary { { "SqlDefaultValue", "getdate()" }, } }, });` Why? Thanks in advance. – RamNow Jul 17 '18 at 13:12
  • Be aware that doing this causes the INSERT sql of `ctx.SaveChanges()` for your model to OVERRIDE values passed in on the model. It literally replaces your model's values as soon as you try to save it. You have to insert, then update, your model. The problem occurs in EF Core too, and is a known issue w/o a solution: https://github.com/aspnet/EntityFrameworkCore/issues/7089 – Barry Oct 09 '19 at 17:11
30

Your model properties don't have to be 'auto properties' Even though that is easier. And the DefaultValue attribute is really only informative metadata The answer accepted here is one alternative to the constructor approach.

public class Track
{

    private const int DEFAULT_LENGTH = 400;
    private int _length = DEFAULT_LENGTH;
    [DefaultValue(DEFAULT_LENGTH)]
    public int LengthInMeters {
        get { return _length; }
        set { _length = value; }
    }
}

vs.

public class Track
{
    public Track()
    {
        LengthInMeters = 400;   
    }

    public int LengthInMeters { get; set; }        
}

This will only work for applications creating and consuming data using this specific class. Usually this isn't a problem if data access code is centralized. To update the value across all applications you need to configure the datasource to set a default value. Devi's answer shows how it can be done using migrations, sql, or whatever language your data source speaks.

Community
  • 1
  • 1
calebboyd
  • 5,744
  • 2
  • 22
  • 32
  • 26
    Note: This will not set a default value *in the database*. Other programs not using your entity will not get that default value. – Eric J. Aug 18 '14 at 22:45
  • This part of the answer, but it doesn't work if you are inserting records in ways other than through Entity Framework. Also if you are creating a new non-null column on a table, this will not give you the ability to set the default value for existing records. @devi has a valuable addition below. – d512 Feb 24 '15 at 04:50
  • Why is your first approach the "correct" way? Will you run into unintended issues with the constructor approach? – Ebsan Mar 25 '15 at 23:51
  • a matter of opinion, and those change :) And probably not. – calebboyd Mar 26 '15 at 21:25
  • 2
    Under certain circumstances, I've had issues with the constructor approach; setting a default value in the backing field seems to be the least invasive solution. – Lucent Fox Jul 18 '15 at 15:06
16

What I did, I initialized values in the constructor of the entity

Note: DefaultValue attributes won't set the values of your properties automatically, you have to do it yourself

Sameh Deabes
  • 2,960
  • 25
  • 30
  • 6
    The problem with the constructor setting the value is that EF will do an update to the database when committing the transaction. – Luka Jun 17 '15 at 11:53
  • The model first crete the default values by this way – Lucas Oct 14 '16 at 17:03
  • The DefaultValue is a species that lives in far away, foreign lands, too shy to encounter your properties by itself. Do not make a sound, it is easily scared away - should it ever come near enough to hear it. +1 for stating the not at all obvious. – Risadinha Jun 17 '19 at 14:08
10

I admit that my approach escapes the whole "Code First" concept. But if you have the ability to just change the default value in the table itself... it's much simpler than the lengths that you have to go through above... I'm just too lazy to do all that work!

It almost seems as if the posters original idea would work:

[DefaultValue(true)]
public bool IsAdmin { get; set; }

I thought they just made the mistake of adding quotes... but alas no such intuitiveness. The other suggestions were just too much for me (granted I have the privileges needed to go into the table and make the changes... where not every developer will in every situation). In the end I just did it the old fashioned way. I set the default value in the SQL Server table... I mean really, enough already! NOTE: I further tested doing an add-migration and update-database and the changes stuck. enter image description here

Anthony Griggs
  • 1,469
  • 2
  • 17
  • 39
9

After @SedatKapanoglu comment, I am adding all my approach that works, because he was right, just using the fluent API does not work.

1- Create custom code generator and override Generate for a ColumnModel.

   public class ExtendedMigrationCodeGenerator : CSharpMigrationCodeGenerator
{

    protected override void Generate(ColumnModel column, IndentedTextWriter writer, bool emitName = false)
    {

        if (column.Annotations.Keys.Contains("Default"))
        {
            var value = Convert.ChangeType(column.Annotations["Default"].NewValue, column.ClrDefaultValue.GetType());
            column.DefaultValue = value;
        }


        base.Generate(column, writer, emitName);
    }

}

2- Assign the new code generator:

public sealed class Configuration : DbMigrationsConfiguration<Data.Context.EfSqlDbContext>
{
    public Configuration()
    {
        CodeGenerator = new ExtendedMigrationCodeGenerator();
        AutomaticMigrationsEnabled = false;
    }
}

3- Use fluent api to created the Annotation:

public static void Configure(DbModelBuilder builder){    
builder.Entity<Company>().Property(c => c.Status).HasColumnAnnotation("Default", 0);            
}
Denny Puig
  • 632
  • 9
  • 11
7

In .NET Core 3.1 you can do the following in the model class:

    public bool? Active { get; set; } 

In the DbContext OnModelCreating you add the default value.

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Foundation>()
            .Property(b => b.Active)
            .HasDefaultValueSql("1");

        base.OnModelCreating(modelBuilder);
    }

Resulting in the following in the database

enter image description here

Note: If you don't have nullable (bool?) for you property you will get the following warning

The 'bool' property 'Active' on entity type 'Foundation' is configured with a database-generated default. This default will always be used for inserts when the property has the value 'false', since this is the CLR default for the 'bool' type. Consider using the nullable 'bool?' type instead so that the default will only be used for inserts when the property value is 'null'.
Roar Jørstad
  • 105
  • 2
  • 5
6

It's simple! Just annotate with required.

[Required]
public bool MyField { get; set; }

the resultant migration will be:

migrationBuilder.AddColumn<bool>(
name: "MyField",
table: "MyTable",
nullable: false,
defaultValue: false);

If you want true, change the defaultValue to true in the migration before updating the database

Marisol Gutiérrez
  • 1,046
  • 8
  • 6
4

I found that just using Auto-Property Initializer on entity property is enough to get the job done.

For example:

public class Thing {
    public bool IsBigThing{ get; set; } = false;
}
Velyo
  • 262
  • 1
  • 8
  • 6
    You'd think this would work, with code first. But it did not for me with EF 6.2. – user1040323 Oct 11 '18 at 14:09
  • This actually works perfectly for Code First EF 6. I am not sure why negative score for this answer. I gave one upvote. – Maverik Jul 16 '21 at 15:24
  • Does not work with EF Core 5.0.13. I ended up just manually editing the migration to have ..., defaultValue: false); – M Townsend Dec 16 '21 at 22:58
4
using System.ComponentModel;

[DefaultValue(true)]

public bool Active { get; set; }
Ruli
  • 2,592
  • 12
  • 30
  • 40
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 12 '22 at 19:14
3

In EF core released 27th June 2016 you can use fluent API for setting default value. Go to ApplicationDbContext class, find/create the method name OnModelCreating and add the following fluent API.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<YourTableName>()
        .Property(b => b.Active)
        .HasDefaultValue(true);
}
Sracanis
  • 490
  • 5
  • 25
0

Just Overload the default constructor of Model class and pass any relevant parameter which you may or may not use. By this you can easily supply default values for attributes. Below is an example.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Aim.Data.Domain
{
    [MetadataType(typeof(LoginModel))]
    public partial class Login
    {       
        public Login(bool status)
        {
            this.CreatedDate = DateTime.Now;
            this.ModifiedDate = DateTime.Now;
            this.Culture = "EN-US";
            this.IsDefaultPassword = status;
            this.IsActive = status;
            this.LoginLogs = new HashSet<LoginLog>();
            this.LoginLogHistories = new HashSet<LoginLogHistory>();
        }


    }

    public class LoginModel
    {

        [Key]
        [ScaffoldColumn(false)] 
        public int Id { get; set; }
        [Required]
        public string LoginCode { get; set; }
        [Required]
        public string Password { get; set; }
        public string LastPassword { get; set; }     
        public int UserGroupId { get; set; }
        public int FalseAttempt { get; set; }
        public bool IsLocked { get; set; }
        public int CreatedBy { get; set; }       
        public System.DateTime CreatedDate { get; set; }
        public Nullable<int> ModifiedBy { get; set; }      
        public Nullable<System.DateTime> ModifiedDate { get; set; }       
        public string Culture { get; set; }        
        public virtual ICollection<LoginLog> LoginLogs { get; set; }
        public virtual ICollection<LoginLogHistory> LoginLogHistories { get; set; }
    }

}
  • 1
    This suggestion is totally client side logic. This "works" as long as you will only interact with the database using the application. As soon as someone wants to insert a record manually or from another application, you realize the downside that there is no default expression in the schema and this cannot scale to other clients. Although it wasn't explicitly stated, this legitimate topic implies the requirement to get EF to create a migration that places a default expression into the column definition. – Tom Nov 15 '18 at 15:15
-1

Even from .NET Core 1.0, It is possible to set default values when you are using the code first approach. See the following code snippet.

using System.ComponentModel;
private bool _myVal = false;

[DefaultValue(true)]
public bool Active
{
    get
    {
        return _myVal;
    }
    set
    {
        _myVal = value;
    }
}

Read for more: Microsoft official docs

Lakshitha Kanchana
  • 844
  • 2
  • 12
  • 32
-3

Lets consider you have a class name named Products and you have a IsActive field. just you need a create constructor :

Public class Products
{
    public Products()
    {
       IsActive = true;
    }
 public string Field1 { get; set; }
 public string Field2 { get; set; }
 public bool IsActive { get; set; }
}

Then your IsActive default value is True!

Edite :

if you want to do this with SQL use this command :

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Blog>()
        .Property(b => b.IsActive)
        .HasDefaultValueSql("true");
}
Hatef.
  • 534
  • 4
  • 18
-3

The Entity Framework Core Fluent API HasDefaultValue method is used to specify the default value for a database column mapped to a property. The value must be a constant.

public class Contact
{
    public int ContactId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public bool IsActive { get; set; }
    public DateTime DateCreated { get; set; }
}
public clas SampleContext : DbContext
{
    public DbSet<Contact> Contacts { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Context>()
            .Propery(p => p.IsActive)
            .HasDefaultValue(true);
    }
}

Or

like it!

You can also specify a SQL fragment that is used to calculate the default value:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Blog>()
        .Property(b => b.Created)
        .HasDefaultValueSql("getdate()");
}
Nader Gharibian Fard
  • 6,417
  • 4
  • 12
  • 22
-5

Hmm... I do DB first, and in that case, this is actually a lot easier. EF6 right? Just open your model, right click on the column you want to set a default for, choose properties, and you will see a "DefaultValue" field. Just fill that out and save. It will set up the code for you.

Your mileage may vary on code first though, I haven't worked with that.

The problem with a lot of other solutions, is that while they may work initially, as soon as you rebuild the model, it will throw out any custom code you inserted into the machine-generated file.

This method works by adding an extra property to the edmx file:

<EntityType Name="Thingy">
  <Property Name="Iteration" Type="Int32" Nullable="false" **DefaultValue="1"** />

And by adding the necessary code to the constructor:

public Thingy()
{
  this.Iteration = 1;
Acorndog
  • 1
  • 1
-6

Set the default value for the column in table in MSSQL Server, and in class code add attribute, like this:

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]

for the same property.

Fabien
  • 4,862
  • 2
  • 19
  • 33
ngochoaitn
  • 27
  • 4