Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9253669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:16:37+00:00 2026-06-18T11:16:37+00:00

Is it possbile to call Validate(..) without using DbContext ? I want to use

  • 0

Is it possbile to call Validate(..) without using DbContext?

I want to use it in Unit Tests.

If I use TryValidateObject(..) on my Contract object – only the validation of User Property is called, but not Validate(..)

Here is code of my Entity:

[Table("Contract")]

public class Contract : IValidatableObject
{
   [Required(ErrorMessage = "UserAccount is required")]
   public virtual UserAccount User
   {
      get;
      set;
   }

   public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
   {
      ...
   }

   ...
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:16:38+00:00Added an answer on June 18, 2026 at 11:16 am

    Yes,
    you need to call Validator.TryValidateObject(SomeObject,…)
    here is an Example
    http://odetocode.com/blogs/scott/archive/2011/06/29/manual-validation-with-data-annotations.aspx

    … the juicy bit is….

            var vc = new ValidationContext(theObject, null, null);
            var vResults = new List<ValidationResult>();
            var isValid = Validator.TryValidateObject(theObject, vc, vResults, true);
            // isValid has  bool result, the actual results are in vResults....
    

    Let me explain better, You need to have all Annotations Valid BEFORE the Validator will call the validate routine, here I added a test program to illustrate what is most likely your issue

    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    namespace ValidationDemo
    {
    class Program
    {
        static void Main(string[] args)
        {
            var ord = new Order();
            // If this isnt present, the validate doesnt get called since the Annotation are INVALID so why check further...
            ord.Code = "SomeValue";   // If this isnt present, the validate doesnt get called since the Annotation are INVALID so why check further...
            var vc = new ValidationContext(ord, null, null);
            var vResults = new List<ValidationResult>();    // teh results are here
            var isValid = Validator.TryValidateObject(ord, vc, vResults, true);    // the true false result
            System.Console.WriteLine(isValid.ToString());
            System.Console.ReadKey();
        }
    }
    public class Order : IValidatableObject
    {
        public int Id { get; set; }
        [Required]
        public string Code { get; set; }
        public   IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            var vResult = new List<ValidationResult>(); 
            if (Code != "FooBar") // the test conditions here
            {
                {
                    var memberList = new List<string> { "Code" }; // The
                    var err = new ValidationResult("Invalid Code", memberList);
                    vResult.Add(err);
                }
            }
            return vResult;
        }
    }
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to validate a form using an AJAX call to check available inventory.
Is it possible to call CRM 4 webservices using CRM 5 sdk.I have a
it isn't possible to call a model's methods in a template without making your
Is it possible to call a web service from within Flash (using AS3)? Or
I am using jquery validate and the jquery version of tinymce. I found this
I'm validating a user-entered date string in YYYY-MM-DD format using Zend_Validate::is($value,'Date') . This call
I'm tring to use jquery validate to show some errors returned from the server.
I'm trying to use scalaz validation in our project and have ran into a
Long post - sorry.... I'm doing input validation for a WCF service and using
I am using EF5 Code-first and therefore I have a DbContext with a Database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.