ValidationContext is always null RRS feed

  • Question

  • User299025995 posted


    I'm using VS 2012 and I created an Asp.Net Dynamic Data Entities website with Entity Framework 5 DbContext.

    I would like to perform validation reading some data from the DbContext so I created a custom validation attribute and I overriden IsValid method.
    The problem is that validationContext parameter is always null.

    I also tried to override DbContext SaveChanges to perform custom validation but it seems that SaveChanges isn't invoked.

    How can I perform custom validation reading data from DbContext and having entity (validation context) that is being validated?

    Saturday, October 19, 2013 6:48 AM

All replies

  • User-330204900 posted

    Hi OasisLiveFor, I don;t do things liek that I get the saving changes event and then do my validation there are I always had issues doing things the other way.

    Also I'm not sure that the Validation Context is for that can you post some sample code please?

    Saturday, October 19, 2013 7:12 AM
  • User299025995 posted
    public partial class TestDynamicDataEntitiesContext : DbContext
            public TestDynamicDataEntitiesContext(string nameOrConnectionString)
                : base(nameOrConnectionString)
                ObjectContext context = ((IObjectContextAdapter)this).ObjectContext;
                context.SavingChanges += Context_SavingChanges;
            private void Context_SavingChanges(object sender, EventArgs e)
                ObjectContext context = (ObjectContext)sender;
                TestDynamicDataEntities entities = new TestDynamicDataEntities();
                foreach (ObjectStateEntry entry in context.ObjectStateManager.GetObjectStateEntries(EntityState.Modified))
                    if (entry.Entity != null)
                        if (entry.Entity.GetType() == typeof(#MyEntityType#))
    						#MyEntityType# myEntity = entry.Entity as #MyEntityType#;
                            // Perform custom validation on myEntity reading from TestDynamicDataEntities context.


    I created a base class that inherits from DbContext and I changed t4 template to inherits form this custom base class. I attached to SavingChanges and perrmed custom validation.
    Everything works fine.

    Is this approach correct?

    Saturday, October 19, 2013 7:38 AM
  • User697462465 posted

    Hi OasisLive,

    I suggest you to pass the ValidationContext you need to override DbContext.ValidateEntity(). The method takes two parameters - entity entry and items. You would pass your validation context in the items dictionary. Take a look at the "Customizing Validation" section of a blog :


    Hope this helps.

    Best Regards,
    Terry Guo

    Monday, October 21, 2013 3:44 AM
  • User299025995 posted


    ValidateEntity is not invoked.

    As you can see from my previous post, I use DBContext but I register an ObjectContext in global.asax.cs:


     DefaultModel.RegisterContext(() =>
                     return ((IObjectContextAdapter)new TestDynamicDataEntities()).ObjectContext;
    Monday, October 21, 2013 4:40 AM