Reputation: 3373
I wanted to use the [DefaultValue] annotation on my entity to specify a default value that gets stored in the DB when creating and not setting the value for status.
// ApprovalStatus is Enum
// Pending = 2
[DefaultValue(ApprovalStatus.Pending)]
public ApprovalStatus Status { get; set; }
But unfortunately the value stored in the DB when creating and saving the entity is 0 and not 2.
What am I doing wrong?
Thanks and kind regards, Nikolai
Upvotes: 1
Views: 1006
Reputation: 41779
you must use the fluent API, data annotations are not supported in this scenario:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>()
.Property(b => b.Status)
.HasDefaultValue(ApprovalStatus.Pending);
}
see https://docs.efproject.net/en/latest/modeling/relational/default-values.html
Upvotes: 2