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 9169743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:55:15+00:00 2026-06-17T15:55:15+00:00

I am attempting to perform manual validation of a class using DataAnnotations. The application

  • 0

I am attempting to perform manual validation of a class using DataAnnotations. The application is a console app, so MVC is not involved. I am using .NET 4.0.

I got my guidance from this article: the only difference appears to be that I am attempting to use a metadata class. But other things I’ve read suggest that this can be done.

But, at run time, the object passes validation. I’ve used DataAnnotations in MVC3 and thought I was pretty good with them, but I’m baffled.

What am I missing? Is there an assembly other than System.ComponentModel.DataAnnotations that’s required?

/// This is a partial-class addition to an Entity Framework class, so the properties are
/// defined in the EF .designer.cs file.  
[MetadataType(typeof(EntityMetadata.tblThingMetaData ))]
public partial class tblThing
{

}

The metadata class:

public partial class tblThingMetaData
{
    [Required(AllowEmptyStrings = false, ErrorMessage = "Sequence number is required")]
    [RegularExpression("A")]
    public string seq_num { get; set; }
}

The test:

    [TestMethod]
    public void VerifyValidationWorksOnEntities()
    {
        tblThing newThing = new tblThing()
        {
            seq_num = "B"
        };

        List<ValidationResult> results = new List<ValidationResult>();
        bool validationResult = Validator.TryValidateObject(
            newThing,
            new ValidationContext(newThing, null, null),
            results,
            true);

        Assert.AreNotEqual(0, results.Count);
        Assert.IsFalse(validationResult);
    }

I’ve tried other variations: newThing.seq_num being null, validating the seq_num property only, etc. It always passes validation and has no validation results. The test always fails.

Many thanks for any advice you can give me.

  • 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-17T15:55:16+00:00Added an answer on June 17, 2026 at 3:55 pm

    Found an answer here. Apparently, this doesn’t work outside of Silverlight or MVC unless you add the following before validating:

    TypeDescriptor.AddProviderTransparent(
       new AssociatedMetadataTypeTypeDescriptionProvider(
           typeof(tblThing),
           typeof(EntityMetadata.tblThingMetaData)
       ), typeof(tblThing));
    

    Note that the last parameter needs to be typeof(tblThing), not newThing. Even though there’s an overload that takes a single instance of the type you’re associating with the metadata, and even if that’s the same instance you plan to validate, it won’t work if you provide an instance rather than a Type.

    Tiresome, but at least it works now.

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

Sidebar

Related Questions

I'm attempting to create a small console app in C# to perform inserts on
I'm attempting to perform a synchronous write/read in a demux-based client application with MINA
I'm attempting to perform a link of previously generated .obj files (using the latest
I'm attempting to perform a full outer join on two tables that are not
I am attempting to perform SPARQL update queries using fuseki-server.jar with the following code:
I'm attempting to perform a duplicate clean-up query in MySQL, using a contacts table,
I am using mongoid on rails3. I am attempting to perform a very simple
I am attempting to use the Microsoft enterprise Validation methods to perform validation in
Using Ravendb (build 960) I am attempting to perform a bulk update on a
Using an update panel I am attempting to perform an Async postback with a

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.