Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
737 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello,
When the request is made, is there a way to compare with data annotations if the start date is smaller than the end date?
Thank you.
public class OrderHistoryRequestDto
{
[Required(ErrorMessage = "Please add StartDate to the request.")]
[DataType(DataType.Date)]
public DateTime? StartDate { get; set; }
[Required(ErrorMessage = "Please add EndDate to the request.")]
[DataType(DataType.Date)]
public DateTime? EndDate { get; set; }
}
Write custom validation.
public class OrderHistoryRequestDto : IValidatableObject
{
[Required(ErrorMessage = "Please add StartDate to the request.")]
[DataType(DataType.Date)]
public DateTime? StartDate { get; set; }
[Required(ErrorMessage = "Please add EndDate to the request.")]
[DataType(DataType.Date)]
public DateTime? EndDate { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if(EndDate.Value <= StartDate.Value)
{
yield return new ValidationResult("End date must be greater than the start date.", new[] { "EndDate" });
}
}
}
If you want an attribute then write a custom attribute.
There is also the openly published documentation which you can read to figure out if an existing attribute will suit your needs. For example, the compare attribute.