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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:43:29+00:00 2026-06-02T19:43:29+00:00

IValidatableObject.Validate only gets called when the implementing entities DbEntityEntry.State differs from Unchanged. And just

  • 0

IValidatableObject.Validate only gets called when the implementing entities DbEntityEntry.State differs from “Unchanged”. And just changing a navigation property won’t change the state and so the validation will never occur.

Why Microsoft always releases half-baked beta things?

I cannot even detect the navigation property change by hand:

var changes = context.ChangeTracker.Entries()
    .Where(e => e.State != EntityState.Unchanged)
    .ToArray();

Returns an empty array.

  • 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-02T19:43:31+00:00Added an answer on June 2, 2026 at 7:43 pm

    There are a few interesting points here. EntityFramework tracks changes to navigation properties independently on the changes to entities. context.ChangeTracker.Entries() returns only changes to entities and not to relationships. This is the reason why you don’t see these. If you really want to look at relationships and how they changed you can drop down to the ObjectContext and do the following:

    var objectContext = ((IObjectContextAdapter) ctx).ObjectContext;
    foreach(var relationshipEntry in objectContext.ObjectStateManager
                                                  .GetObjectStateEntries(EntityState.Added | EntityState.Modified | EntityState.Deleted)
                                                  .Where(e => e.IsRelationship))
    {
        EntityKey entityKey1, entityKey2;
    
        if (relationshipEntry.State == EntityState.Added)
        {
            entityKey1 = (EntityKey)relationshipEntry.CurrentValues[0];
            entityKey2 = (EntityKey)relationshipEntry.CurrentValues[1];            
        }
        else
        {
            entityKey1 = (EntityKey)relationshipEntry.OriginalValues[0];
            entityKey2 = (EntityKey)relationshipEntry.OriginalValues[1];                        
        }
    
        var entity1 = objectContext.GetObjectByKey((EntityKey)entityKey1);
        var entity2 = objectContext.GetObjectByKey((EntityKey)entityKey2);
    }
    

    The scenario that is more interesting to me is the one when you need to re-validate your entity when the relationship changes. I assume that you don’t have foreign keys – otherwise your entity would have been marked as modified as changing the navigation proprerty would change the value of the foreign key which in turn would mark the entity as modified. Anyways given that I have three valid entities – what is the scenario where changing relationships could make the entity (or the model) invalid? Also, note that validation by itself is only validating the given entity but never follows navigation properties to validate related entities.
    Finally if you really need to validate entities when relationships change I think you have 4 options:

    • try adding foreign keys so that changing relationships will change the foreign key which should mark the entity as modified (disclaimer: I have not tried that)

    • override DbContext.ShouldValidateEntity() method to validate all the entities instead of only modified ones (this is where the filtering logic happens). Note that it may have some negative impact on performance. Here is the code you would need to add to your class derived from DbContext:

            protected  override bool ShouldValidateEntity(DbEntityEntry entityEntry)
            {
                return (entityEntry.State & EntityState.Deleted) == 0;
            }
    
    • override DbContext.SaveChanges() so that you invoke validation for all modified entities and for all entities that are involved in modified relationships (use the code above, you are most likely only interested in relationship entries that have been added)

    • when you modify a collection manually mark the entity as modified. Note that it may cause sending unneeded updates to the database so depending on how many entities you are tracking it may be much cheaper just to validate all but deleted entities

    You can find more details about validation and validation customization here:
    http://blogs.msdn.com/b/adonet/archive/2010/12/15/ef-feature-ctp5-validation.aspx
    http://blogs.msdn.com/b/adonet/archive/2011/05/27/ef-4-1-validation.aspx
    (Yes it’s for the CTP and EF 4.1 but it sill holds)

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

Sidebar

Related Questions

so according to Gu IValidatableObject.Validate() should get called when a controller validates it's model
I am trying to use IValidatableObject to validate form values with respect to each
I have a model that implement IValidatlableObject, and so custom error checking through Validate
I'm implementing validation in my web app... the problem is it seems to be
Each form in the application has a set of radiobuttons. Once selected, only certain
I have a struct that implements IValidatableObject, which I use for properties on a
public abstract class Animal , IValidatableObject { public string Id {get;set;} public string Name
I'm trying to validate a model containing other objects with validation rules using the
I'm trying to make use of the IValidatableObject as described here http://davidhayden.com/blog/dave/archive/2010/12/31/ASPNETMVC3ValidationIValidatableObject.aspx . But
I have the following set of Interfaces and Classes. public interface IValidatableObject { List<string>

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.