In MVC2 Entity Framework 4, server-side validation of a REQUIRED String Property does not function.

asp.net-mvc data-annotations entity-framework

Question

I'm trying to get server-side validation of an Entity Framework String Property to work. Other server-side validation such as data type validation and required dateTime and numeric EF properties are working.

This in VS 2010, .Net 4.0, MVC2 + Cloud, ADO.Net Entity Framework.

The String Property I am having issues with is mapped to a SQL 2008, Varchar(50) non-nullable column.

When I try to post to my Create action with an empty string for this Property, I get the follwing error.

Exception Details: System.Data.ConstraintException: This property cannot be set to a null value.

When I post to the action with a blank space, I successfully get a required field validation message.

I have tried using Data Annotations and ClientSideValidation but there seems to be issues with ClientSideValidation working on partial views and jquery dialogs.

Here is the orginal autogenerated code from the entity framework.

[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.String GradeTypeName
{
    get
    {
        return GradeTypeName;
    }
    set
    {
        OnGradeTypeNameChanging(value);
        ReportPropertyChanging("GradeTypeName");
        _GradeTypeName = StructuralObject.SetValidValue(value, false);
        ReportPropertyChanged("GradeTypeName");
        OnGradeTypeNameChanged();
    }
}

Depending on the signature of the Action method (CREATE or EDIT), the exception can occur before stepping into the method or within the method when UpdateModel() is called. The inner exception is at the line below from the model.designer.cs file.

_GradeTypeName = StructuralObject.SetValidValue(value, false);

I have been able to reproduce this on a simple mvc2 web application.

1
14
4/6/2013 10:06:16 PM

Accepted Answer

i was having the same problem for a while. I have found a piece of explanation here: http://mvcmusicstore.codeplex.com/workitem/6604 . To put it in a nutshell, the exception "System.Data.ConstraintException: This property cannot be set to a null value" is thrown by Entity's Property Validation. This validation is performed when your mvc application tries to bind the form field to the corresponding entity property( it's called PreBinding Validation, and it occurs when submitting the form). As the field is empty( therefore convert to null), the binder tries to bind a null value to the property, which violates the Non-Null constraint on your entity's property.

But if you post with a blank field ( that is different from empty, therefore null) Entity validation passes( as the property is not set to a null value anymore), and then your see the message from the "Required" annotation validation, that is performed after the prebinding (it's PostBinding Validation).

A workaround is to use the annotation [DisplayFormat(ConvertEmptyStringToNull = false)] that tells to the binder not to convert an empty string to null.

  [Required]
  [DisplayFormat(ConvertEmptyStringToNull = false)]
  public string YourStringProperty { get; set;}

Hope, this helps!

30
8/3/2012 8:36:47 PM

Popular Answer

This was very helpful. I'm using MVC3 and entity framework. I was passing my entities directly into the controller, but got the same error when the form was blank. With entity framework you can do data validation by editing the auto-generated code, but creating a separate partial class of the entity worked better for me.

[MetadataType(typeof(TestEntityValidation))]
public partial class TestEntity{
}

public class TestEntityValidation{
    [Required]
    [DisplayFormat(ConvertEmptyStringToNull = false)]
    public String name { get;set}
}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow