Mixing Fluent API and DataAnnotations in EF code first
You can use Data Annotation attributes and Fluent API at the same time. Entity Framework gives precedence to Fluent API over Data Annotations attributes.
You can use Data Annotation attributes and Fluent API at the same time. Entity Framework gives precedence to Fluent API over Data Annotations attributes.
MVC3 has a new jQuery Validation mechanism that link jQuery Validation and Validation Attributes Metadata, this is the jquery.validate.unobtrusive file that takes all data- attributes and work with them, just like before when you set the <add key=”UnobtrusiveJavaScriptEnabled” value=”false” /> All you need to do is come up with your own Custom Validation Attributes, for … Read more
I don’t know if this will satisfy your DRY needs, but it’s a start I think. I would rework the model a bit like this: public class Person { private const int DEFAULT_AGE = 18; private int _age = DEFAULT_AGE; [DefaultValue(DEFAULT_AGE)] public int Age { get { return _age; } set { _age = value; … Read more
I appreciate the effort that went into the existing answer, but I haven’t found it actually answering the question… so I tested this, and found out that [Column(TypeName = “ntext”)] public string Body { get; set; } (the one from System.ComponentModel.DataAnnotations) will work to create an ntext type column. (My problem with the accepted answer … Read more
Use the Range Attribute. Set minimum to 1 and maximum to int.MaxValue [Range(1, int.MaxValue, ErrorMessage = “Value for {0} must be between {1} and {2}.”)]
I have extended Zhaph answer to support grouping of properties. [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] public class AtLeastOnePropertyAttribute : ValidationAttribute { private string[] PropertyList { get; set; } public AtLeastOnePropertyAttribute(params string[] propertyList) { this.PropertyList = propertyList; } //See http://stackoverflow.com/a/1365669 public override object TypeId { get { return this; } } public override bool IsValid(object value) { … Read more
If you can’t get it working on the model, you could try it on the view. @Html.TextBoxFor(m => m.ValidFrom, “{0:dd/MM/yyyy}”, new {maxlength = 10})
RequiredIf validation attribute I’ve written a RequiredIfAttribute that requires a particular property value when a different property has a certain value (what you require) or when a different property has anything but a specific value. This is the code that may help: /// <summary> /// Provides conditional validation based on related property value. /// </summary> … Read more
Try tagging it with: [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = “{0:MM/dd/yyyy}”)]
DisplayName sets the DisplayName in the model metadata. For example: [DisplayName(“foo”)] public string MyProperty { get; set; } and if you use in your view the following: @Html.LabelFor(x => x.MyProperty) it would generate: <label for=”MyProperty”>foo</label> Display does the same, but also allows you to set other metadata properties such as Name, Description, … Brad Wilson … Read more