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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:49:45+00:00 2026-05-22T15:49:45+00:00

I want to use a validation anotation. I have already a model implemented by

  • 0

I want to use a validation anotation.
I have already a model implemented by entity framework.
I need data validation and I know that there is a data anotation which is very nice…
But I really don’t know how can I use it with entity framework correct.

Should I edit entities? Or should I write separated class? Or should I write class which are inherit from entities?

Can you tell me the best way I should use?

I want to write as few code as it is possible.

  • 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-22T15:49:46+00:00Added an answer on May 22, 2026 at 3:49 pm

    The way to handle this is a combination of partial classes and a special attribute that allows you to attach metadata to another class.

    The entity framework already helps you out here by generating all classes as partial classes. So, if you had an entity in your model called Settings, the EF would create this:

    public partial class Setting : INotifyPropertyChanging, INotifyPropertyChanged
    {
        // Auto-gen Properties, methods, etc go here.
    }
    

    This means you can have any number of other partial class Setting fragments in other files, which the EF will not touch if/when you regenerate that code. If you want to add data validation attributes to this, it takes two steps:

    1. Attach a MetadataType attribute to the Setting class. There doesn’t need to be anything in the body of this partial class fragment, it’s only there to associate the attribute.

      [MetadataType(typeof(SettingMetadata))]   
      public partial class Setting
      {
      }
      
    2. Create a second class that has the same public field names as the EF class, and associate whatever data validation attributes you want. The compiler will match up the metadata class fields with the EF class fields by name, and act as it whatever metadata is attached to your second class is also on your first class.

      public class SettingMetadata
      {
          [Display(Name="Base Rate")]
          [Required]
          public decimal Rate
          {
              get;
              set;
          }
      
          [Display(Name = "Permit Payments")]
          public Boolean AllowPayments
          {
              get;
              set;
          }
      
          [Display(Name = "Base URL For Web Service")]
          [Required]
          [SuppressMessage("Microsoft.Design", "CA1056:UriPropertiesShouldNotBeStrings", Justification = "Type must match linked metadata type.")]
          public string WebServiceUrl
          {
              get;
              set;
          }
      }
      

    As mentioned in the FxCop suppression message, the name and type of the fields must match between the metadata class and associated class for this to work.

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

Sidebar

Related Questions

Hi have some forms that I want to use some basic php validation (regular
I want to use the MultipleLookupField control in a web page that will run
I have a requirement that the validation error messages that are passed to my
I have two fields of an entity class which I don't want to be
I would like use data annotations to handle validation in my Silverlight app. The
I have a big validation JS script. I want to put this script to
I want to use a temp directory that will be unique to this build.
I want to use a validator to ensure 2 password fields match in Flex.
Below is my stored procedure. I want use stored procedure select all row of
I want to use the mouse scrollwheel in my OpenGL GLUT program to zoom

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.