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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:05:54+00:00 2026-05-23T08:05:54+00:00

I want to validate each entity when saving. That is, I have some custom

  • 0

I want to validate each entity when saving. That is, I have some custom C# function in each entity class that validates the data.

How can I do that? I don’t want to use database constraints because they cannot express my constraints.

Should I implement some interface???

  • 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-23T08:05:54+00:00Added an answer on May 23, 2026 at 8:05 am

    Entity framework 3.5 and 4.0 offers even called SavingChanges. Entity framework 4.0 and 4.1 has SaveChanges method virtual (as already mentioned).

    You can either override method or use event handler and write code like this for 3.5 and 4.0:

    var entities = context.ObjectStateManager
                          .GetObjectStateEntries(EntitiState.Modified | EntityState.Added)
                          .Where(e => !e.IsRelationship)
                          .Select(e => e.Entity)
                          .OfType<YourEntityType>();
    
    foreach(var entity in entities)
    {
        entity.Validate();
    }
    

    In DbContext API (EF 4.1) you must use

    var entities = context.ChangeTracker
                          .Entries<YourEntityType>()
                          .Where(e.State == EntityState.Added || e.State == EntityState.Modified)
                          .Select(e => e.Entity);
    
    foreach(var entity in entities)
    {
        entity.Validate();
    }
    

    You can use custom interface implemented by your entities which will expose Validate method.

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

Sidebar

Related Questions

I have some MET data I want to validate which would look something like
i want to validate all inputs using class name i'm using following code $('#submitbutton').click(function()
I have multiple inputs of text within a form and want to validate each
I have a case where I want to validate an entered vat number. Each
I want to validate below data using regex and python. Below is the dump
I want to validate a registration form that accepts an email address in the
If I want to validate that a text box contains an integer greater than
in my application i want to validate the telephone numbers, how can i write
Given a table of models 'A', that can have multiple child models 'B', of
I want to validate number in range of 0-255 I have this expression '/^([0-1]?[0-9]?[0-9])|([2][0-4][0-9])|(25[0-5])$/'

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.