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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:14:16+00:00 2026-06-14T09:14:16+00:00

By default, EF uses EntityObject when generating the objects. I’ve modified it to use

  • 0

By default, EF uses EntityObject when generating the objects. I’ve modified it to use my own AbstractEntityObject class. In doing so, I’ve added IValidatableObject as I was under the impression when you called context.SaveChanges() it would automatically call Validate and throw the exceptions.

Here is what I have:

public abstract class AbstractEntityObject : EntityObject, IValidatableObject
{
    private readonly DBTable table;

    protected AbstractEntityObject(DBTable table)
    {
        this.table = table;
    }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        List<OITValidationResult> results = new List<OITValidationResult>();
        List<PropertyInfo> properties = new List<PropertyInfo>(GetType().GetProperties());

        foreach (DBField field in table.GetFields())
        {
            foreach (PropertyInfo prop in properties)
                if (StringUtilities.EqualsIgnoreCase(field.FieldName, prop.Name))
                {
                    results.AddRange(field.Validate(prop.GetValue(this, null)));
                    results.AddRange(AdditionalValidation(field, prop));
                    properties.Remove(prop);
                    break;
                }
        }

        return results;
    }

    public abstract List<OITValidationResult> AdditionalValidation(DBField field, PropertyInfo prop);
}

public abstract class AbstractTLMSEntityObject : AbstractEntityObject
{
    protected AbstractTLMSEntityObject(DBTable table) 
        : base(table)
    {
    }

    public override List<OITValidationResult> AdditionalValidation(DBField field, PropertyInfo prop)
    {
        List<OITValidationResult> results = new List<OITValidationResult>();

        if (!EntityState.Equals(EntityState.Unchanged))
        {
            if (StringUtilities.EqualsIgnoreCase(field.FieldName, "userid"))
                prop.SetValue(this, TLMSDB.User.UserName, null);
            else if (StringUtilities.EqualsIgnoreCase(prop.Name, "dtmod"))
                prop.SetValue(this, DateTime.Now, null);
        }

        OITValidationResult additionalResult = AdditionalValidation(field.Field);
        if (additionalResult != null)
            results.Add(additionalResult);

        return results;
    }

    /* By default there is no additional validation, subclasses should override this if they need additional validation */
    public virtual OITValidationResult AdditionalValidation(Enum field)
    {
        return null;
    }
}

Then, I have the subclass, which is a partial class along with the one that EF creates:

public partial class CSDCrud
{
    public enum Fields
    {
        RECEIPT_NUMBER,
        GRANT_ID,
        GRANT_FUND,
        TOTAL_ACRES
    }

    public CSDCrud() : base(CSDCrudDAO.Instance.Table)
    {
    }

    public static String GetDataPropertyName(Enum field)
    {
        return CSDCrudDAO.Instance.Table.GetField(field).FieldName;
    }
}

CSDCrud inherits from AbstractTLMSEntityObject thanks to me changing that in the .tt file.

Now here is where it gets weird. I have set it up so the DBField (referenced in the parent class) does self validation of the data. In this case, I have set it up so if receipt_number is required and thus will fail and throw an exception…In fact, the following occurs…

List<OITValidationResult> results = (List<OITValidationResult>)crudObject.Validate(null);
try
{
    CommonEntityManager.GetContext().CrudSet.AddObject(crudObject);
    CommonEntityManager.GetContext().SaveChanges();
    return true;
}
catch (Exception ex)
{
    return false;
}

As expected, results contains 1 item which is the proper error…however the SaveChanges saves it out just fine, without throwing an exception…what am I missing?

edit: I should note, obviously I can use the SavingChanges event to add in my own handler, but I’m hoping to use the pre-existing infrastructure.

static void context_SavingChanges(object sender, EventArgs e)
    {
        foreach (ObjectStateEntry ose in context.ObjectStateManager.GetObjectStateEntries(EntityState.Modified))
        {
            List<ValidationResult> results = new List<ValidationResult>(((IValidatableObject)ose.Entity).Validate(null));
            if (results.Count > 0)
                throw new CustomException(results);
        }

        foreach (ObjectStateEntry ose in context.ObjectStateManager.GetObjectStateEntries(EntityState.Added))
        {
            List<ValidationResult> results = new List<ValidationResult>(((IValidatableObject)ose.Entity).Validate(null));
            if (results.Count > 0)
                throw new CustomException(results);
        }
    }
  • 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-14T09:14:17+00:00Added an answer on June 14, 2026 at 9:14 am

    Built-in validation is invoked only when you are using DbContext APIs and not ObjectContext APIs.

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

Sidebar

Related Questions

Checkstyle's AbstractClassName check uses the following default pattern to check for class names that
Spring Security ACL plugin for grails by default uses the BasePermission class with 4
Is it possible to forward declare a class that uses default arguments without specifying
I want IDEA to use whatever default system encoding is, instead it uses whatever
Is it good practice to have a class constructor that uses default parameters, or
I am planning to use JGroups in a web application. JGroups by default uses
Default JVM uses maximum 1.5 GB RAM/JVM Java application. But my Server have 8
Single Application Page Asp.net MVC 4 temlplate uses default database to check Login and
Assuming MyClass uses the default destructor (or no destructor), and this code: MyClass *buffer
I've a very basic ASP.NET MVC application that uses the default routing. Now I

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.