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

  • SEARCH
  • Home
  • 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 4018942
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:02:43+00:00 2026-05-20T10:02:43+00:00

I have an object in a simple test scenario that uses EF Code First

  • 0

I have an object in a simple test scenario that uses EF Code First and implements IValidatableObject. There’s some very simple logic that adds a validation error and returns it back. There are also other validations on the object.

However, when saving the object – while the attribute based validations work – the IValidatableObject interface never seems to fire. Debugger doesn’t step into it and the error never shows up with calling SaveChanges() or GetValidationErrors().

public class Customer : IValidatableObject {
[Key]
public int Id { get; set; }

[StringLength(50)]
[DisplayName("First Name")]

public string FirstName { get; set; }

[Required]
[DisplayName("Last Name")]
[StringLength(50)]
public string LastName { get; set; }

[Required]
[StringLength(100)]
public string Company { get; set; }

[StringLength(200)]
public string Email { get; set; }

[DisplayName("Credit Limit")]
public decimal CreditLimit { get; set; }

[DisplayName("Entered On")]
public DateTime? Entered { get; set; }


public virtual ICollection<Address> Addresses { get; set;  }


public Customer()
{
    Entered = DateTime.Now;
    CreditLimit = 1000.00M;

    Addresses = new List<Address>();
}

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
    var results = new List<ValidationResult>();

    // add an error ALWAYS for testing - it doesn't show up
    // and the debugger never hits this code
    results.Add(new ValidationResult("Validate Added message",new [] { "Company" }));

    return results;
}

When I now try to add a customer and check for validation errors:

public void AddNewCustomer()
{
    Customer customer = new Customer();  

    context.Customers.Add(customer);

    customer.LastName = "Strahl";
    customer.FirstName = "Rick";
    customer.Entered = DateTime.Now;
    //customer.Company = "West Wind"; // missing causes val error

    var errorEntries = context.GetValidationErrors();
}

I get ONE validation error for the company, but nothing from the IValidatableObject which should ALWAYS fail.

Any idea why?

  • 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-05-20T10:02:43+00:00Added an answer on May 20, 2026 at 10:02 am

    Quote from Jeff Handley’s Blog Post on Validation Objects and Properties with Validator:

    When validating an object, the
    following process is applied in
    Validator.ValidateObject:

    1. Validate property-level attributes
    2. If any validators are invalid, abort validation returning the
      failure(s)
    3. Validate the object-level attributes
    4. If any validators are invalid, abort validation returning the
      failure(s)
    5. If on the desktop framework and the object implements
      IValidatableObject, then call its
      Validate method and return any
      failure(s)

    This indicates that what you are attempting to do won’t work out-of-the-box because the validation will abort at step #2.

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

Sidebar

Related Questions

I have a simple object that get's geocoding data from the Google Maps API
I have a code sample that gets a SEL from the current object, SEL
I have seen sample source code around that uses different ways of releasing/dealloc'ing objects,
I have a simple test, where I am trying to update the object, but
Pretty simple question: When i have a persistable object, it usually has a property
I'm relatively new to the Component Object Model specification - I have a simple
I have an object that is mapped to a cookie as a serialized base-64
I have an object in a multi-threaded environment that maintains a collection of information,
I have a data object -- a custom class called Notification -- that exposes
I have a nontrivial service object developed with TDD. It started with a simple

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.