Entity Framework 4.1: Override IEnumerable<ValidationResult> Validate

泄露秘密 提交于 2019-12-04 07:46:36

In your code sample you did not derive your dog class from Animal. The animal's validation method will only be called if you iterate through the result set:

public class Dog : Animal
{
  public override IEnumerable<ValidationResult> Validate(ValidationContext      validationContext)
  {
     foreach(var result in base.Validate(validationContext))
     {
     }

     //dog specific validation follows here...
  }
}

Only calling base.Validate() without iterating through the returned collection will not call the base's validation method. Hope, this helps.

public class Dog : Animal
{
  public override IEnumerable<ValidationResult> Validate(ValidationContext      validationContext)
  {
     foreach(var result in base.Validate(validationContext).ToList())
     {
     }

     //dog specific validation follows here...
  }
}

You need to call ToList() since the base method returns IEnumerable type data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!