日期时间值顺序约束

时间:2016-06-09 13:40:29

标签: c# entity-framework constraints

我想制定一个约束,以确保Beginning小于End。我使用Entity Framework,据我所知它不能通过注释来完成。可以用OnModelCreating方法完成吗?

 public class Duty {
        public int Id { get; set; }
        public virtual Employee Employee { get; set; }
        public virtual Store Store { get; set; }
        public DateTime Beginning { get; set; }
        public DateTime End { get; set; }
    }

4 个答案:

答案 0 :(得分:2)

    public class SomeEntity
    {
        [MustBeLessThanDate(nameof(End))]
        public DateTime Beginning { get; set; }

        public DateTime End { get; set; }
    }

    public class MustBeLessThanDateAttribute : ValidationAttribute
    {
        private readonly string _otherPropertyName;

        public MustBeLessThanDateAttribute(string otherPropertyName)
        {
            _otherPropertyName = otherPropertyName;
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var containerType = validationContext.ObjectInstance.GetType();

            var field = containerType.GetProperty(_otherPropertyName);

            var otherValue = (DateTime) field.GetValue(validationContext.ObjectInstance, null);

            var thisValue = (DateTime) value;

            return thisValue < otherValue
                ? ValidationResult.Success
                : new ValidationResult("Value is not less than other value");
        }
    }

答案 1 :(得分:1)

您可以使用自定义验证,方法是从ValidationAttribute类派生,该类具有您可以覆盖的IsValid方法。您可以在viewModel中创建此类,然后在enddate上应用created属性。

答案 2 :(得分:0)

An answer for the same is here.

OR

尝试浏览FluentValidation

它使用流畅的接口和lambda表达式来构建验证规则。

从文档中,您可以通过NuGet将其包含在 -

Install-Package FluentValidation

实施约束非常简单:

using FluentValidation;

public class CustomerValidator: AbstractValidator<Customer> {
  public CustomerValidator() {
    RuleFor(customer => customer.Surname).NotEmpty();
    RuleFor(customer => customer.Forename).NotEmpty().WithMessage("Please specify a first name");
    RuleFor(customer => customer.Discount).NotEqual(0).When(customer => customer.HasDiscount);
    RuleFor(customer => customer.Address).Length(20, 250);
    RuleFor(customer => customer.Postcode).Must(BeAValidPostcode).WithMessage("Please specify a valid postcode");
  }

  private bool BeAValidPostcode(string postcode) {
    // custom postcode validating logic goes here
  }
}

Customer customer = new Customer();
CustomerValidator validator = new CustomerValidator();
ValidationResult results = validator.Validate(customer);

bool validationSucceeded = results.IsValid;
IList<ValidationFailure> failures = results.Errors;

试一试。 (我没有根据您的需求自定义示例,您可能需要深入阅读documentation here。)

答案 3 :(得分:0)

为什么不在您的财产中使用条件。这样的事情......

public int Id { get; set; }
public virtual Employee Employee { get; set; }
public virtual Store Store { get; set; }
public DateTime Beginning { get; set; }

private DateTime _end;
public DateTime End {
    get
    {
        return _end;
    }
    set
    {
        if (value < Beginning)
        {
            throw new Exception("End date can not be less than beginning date.");
        }
        _end = value;
    }
}
  

但在分配beginning date之前不要忘记分配end date