4

I have a EF Code First setup, I have a particular column that is marked as Required via annotation. This column has a default value constraint specified in the database.

What I'm trying to do is allow the user to enter the value on a form but if they omit it, use the default value from the database. I'm not having any luck.

If I don't enter anything, the Required error shows on the form. If I add [DatabaseGenerated(DatabaseGeneratedOption.Computed)] annotation, the value the user enters is ignored and it always uses the database default.

6
  • 1
    The [Required] attribute does not make much sense in this case. If you have a default value, there is no point "pretending" the field to be mandatory. Commented Aug 25, 2016 at 5:18
  • I agree with you, but because this is a string, if I don't specify [Required] the varchar field becomes nullable in the database (which I don't want). Commented Aug 25, 2016 at 12:08
  • 1
    This is a known issue in EF. As far as I know this is fixed in EF 7. Refer data.uservoice.com/forums/…. At the moment your only choice is to declare a "default constraint" using TSQL. Sorry, I thought you knew about it already. Commented Aug 25, 2016 at 22:46
  • @Brad why you're not giving any feedback about the below solutions ? tell us ,we can remove it if it's not helpful to you. Commented Aug 26, 2016 at 0:55
  • @Sampath Because neither of the answers are acceptable answers. Commented Aug 26, 2016 at 1:03

2 Answers 2

0

What you can do is perform the magic while parsing form values.

When checking for the specific column's user input from the form, check the column using

if (!String.IsNullOrWhiteSpace(value))
    //set the value in the database entry based on user's input

The above piece of code only enters a value into the database if something has been entered on the form.

This solution only works if you are able to parse the form's values after user 'submits'.

Sign up to request clarification or add additional context in comments.

1 Comment

If the database has a default constraint, this check is completely unnecessary.
0

This was my solution to this issue, which is not great, but manageable.

In my model, I annotated the property with:

[DefaultValueSql("'A'")]
[DefaultValue("A")]
[Required]

The DefaultValueSql is a custom attribute that handles the creation of SQL default column values when doing migrations. The DefaultValue matches that value. I'm aware this is a violation of DRY, but using this solution removes the need for the default in the database anyway (I'll probably remove it in the future).

In my Controller, on the Create/Edit ActionResult post handlers I added this:

    if (model.ActiveIndicator == null)
    {
        model.ActiveIndicator = ((DefaultValueAttribute)(model.GetType().GetProperty("ActiveIndicator").GetCustomAttributes(typeof(DefaultValueAttribute), true).First())).Value.ToString();
        ModelState["ActiveIndicator"].Errors.Clear(); // Removes Model Error
    }

This sets the default to the DefaultValue listed in the annotation and removes the ModelState error for this property allowing the ModelState.IsValid to be true.

This solution works well for string defaults, however non-string (types that can't be null), should probably be done differently, perhaps in the model class constructor.

Edit: If you are using TryUpdateModel, you must set the ModelState value instead of the submitted model value:

    if (model.ActiveIndicator == null)
    {
        var defValue = ((DefaultValueAttribute)(model.GetType().GetProperty("ActiveIndicator").GetCustomAttributes(typeof(DefaultValueAttribute), true).First())).Value.ToString();
        ModelState.SetModelValue("ActiveIndicator", new ValueProviderResult(defValue, "", CultureInfo.InvariantCulture));
        ModelState["ActiveIndicator"].Errors.Clear(); // Removes Model Error
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.