locked
context_SavingChanges validations RRS feed

  • Question

  • User1980140492 posted

    Heya,

    I'd like to add some extra validations before the user can insert/edit certain table their records.

    I've been trying to do this in my context_SavingChanges in my partial entitiesmodel class like this, but I need it to show the error messages to the user in the validation summary where all the usual validations go to. I know I've seen Stephen do this somewhere ... but I can't find the post :(

    partial void OnContextCreated()
    {
           this.SavingChanges += new EventHandler(context_SavingChanges);
    }
    
            
    
            private static void context_SavingChanges(object sender, EventArgs e)
            {
                foreach (ObjectStateEntry entry in
                ((ObjectContext)sender).ObjectStateManager.GetObjectStateEntries(
                EntityState.Added | EntityState.Modified))
                {
                    if (!entry.IsRelationship && (entry.Entity.GetType() == typeof(AM_ARTICLE_RELATION)))
                    {
                        AM_ARTICLE_RELATION CurrentEntity = (AM_ARTICLE_RELATION)entry.Entity;
    
                        if (!CurrentEntity.AM_SITE.SiteName.Equals(CurrentEntity.AM_ARTICLE.AM_SITE.SiteName) ||
                            !CurrentEntity.AM_SITE.SiteName.Equals(CurrentEntity.AM_ARTICLE1.AM_SITE.SiteName))
                        {
                            throw new ValidationException("Pop Error here ... ");
                            
                        }                    
                    }
                }

    Regards,
    - Yannick

    Wednesday, July 13, 2011 4:13 AM

Answers

  • User-330204900 posted

    Looks good to me,

    private static void context_SavingChanges(object sender, EventArgs e)
    {
        var objects = ((ObjectContext)sender).ObjectStateManager;
        var OC = new Entities();
    
        foreach (ObjectStateEntry entry in objects.GetObjectStateEntries(EntityState.Added))
        {
            if (entry.Entity != null)
            {
                // auto update the EANo
                if (entry.Entity.GetType() == typeof(Customer))
                {
                    var cust = entry.Entity as Customer;
    
                    if (String.IsNullOrEmpty(cust.Description))
                        throw new ValidationException("Invalid Customer Description");
                }
            }
        }
    }

    I also do this to catch all Errors and wrap then (I do a bit more before release like log the error if is is an unkown error and then throw a validation exception with a nice message)

    public override int SaveChanges(System.Data.Objects.SaveOptions options)
    {
        try
        {
            return base.SaveChanges(options);
        }
        catch (Exception exception)
        {
            // Find the most innermost exception, unwrap it.
            var message = exception.ExtractExceptionMessage();
    
            //TODO: build logging and notification in here
            throw new ValidationException(message);
        }
    }

    My ExtractExceptionMessage() is just an extension method to unwrap the inner exception

    public static class ExceptionExtensionMethods
    {
        public static String ExtractExceptionMessage(this Exception exception)
        {
            while (exception.InnerException != null)
                exception = exception.InnerException;
            return exception.Message;
        }
    }

    Hope that helps

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 13, 2011 4:29 AM