0

I have a Entity Framework code first approach, i am using DefaultValue constraint using [DefaultValue("1"), DatabaseGenerated(DatabaseGeneratedOption.Computed)] on a bool field, my problem is even if i set any value to it, it ignores it and sets the value from DefaultConstraint.

Here's my model:

[DefaultValue("1"), DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public bool IsFirstLogin { get; set; }

Here's my update function:

public void Update(out bool isEmailUpdated, Guid? userID = null)
        {
            var user = db.Users.Find(userID);
            user.IsFirstLogin = false;
            isEmailUpdated = false;
            db.SaveChanges();
        }

UPDATES
My concern is not about setting DefaultValue, it's properly being created, my concern is now when i try to set any value to the field, it does not updates with that value, means it automatically sets the Default value to the field. So my problem is with the updates.

Abbas
  • 4,948
  • 31
  • 95
  • 161
  • Have you tried a default initializer instead? `public bool IsFirstLogin { get; set; } = true;` – Andrew Williamson Mar 30 '18 at 06:39
  • 1
    Possible duplicate of [How to set a default value on a Boolean in a Code First model?](https://stackoverflow.com/questions/40936566/how-to-set-a-default-value-on-a-boolean-in-a-code-first-model) – Omar Muscatello Mar 30 '18 at 06:47
  • Please check this link for details. Default value attribute does not help in setting the property value. https://stackoverflow.com/questions/1980520/defaultvalue-attribute-is-not-working-with-my-auto-property – JyothiJ Mar 30 '18 at 06:44

0 Answers0