none
Odd Validation Problem with Child Entities RRS feed

  • Question

  • Hi all,

    I have an application for a customer where there are two entities that look like this:

     

      public class Menu
        { // Native properties
    
            [Key]
            public Guid ID { get; set; }
    
            [Required]
            [StringLength(30)]
            public string InternalName
            {
                get
                {
                    return _InternalName.TrimIfNotNull();
                }
                set
                {
                    _InternalName = value.TrimIfNotNull();
                }
            }
            private string _InternalName;
    
            public MultiLanguage Title { get; set; }
    
            [Required]
            public short SortOrder { get; set; }
    
            [Required]
            public bool IsEnabled { get; set; }
    
            [Required]
            public DateTime CreatedOn { get; set; }
            [Required]
            public DateTime ModifiedOn { get; set; }
    
            // Navigation properties
    
            [Required(ErrorMessage="Menu Restaurant is required.")]
            public virtual Restaurant Restaurant { get; set; }
    
            public virtual ICollection<MenuItem> MenuItems { get; set; }
    
            public virtual Image Image { get; set; }
    
        }
    
    }
      
      public class Image
        {
            // Native properties
    
            [Key]
            public Guid ID { get; set; }
    
            public short Height { get; set; }
            public short Width { get; set; }
            public int Filesize { get; set; }
    
            [StringLength(50)]
            public string Fileformat { get; set; }
    
            public byte[] Picture { get; set; }
    
            [Required]
            public bool IsEnabled { get; set; }
    
            [Required]
            public DateTime CreatedOn { get; set; }
            [Required]
            public DateTime ModifiedOn { get; set; }
    
            // Navigation properties
    
            [Required]
            public virtual ImageType ImageType { get; set; }
    
            public virtual Restaurant Restaurant { get; set; }  // Owner (if Null: image is public)
    
            public virtual Skin Skin { get; set; }              // If Null: image is a menu icon or item picture, but not a skin element
        }

    There's a routine that loads up a menu and then attaches an image and saves.

    The problem is that when the SubmitChanges occurs a validation occurs and it complains that Restaurant is requried. 
    The message displayed is from the Menu entity (the message is set explicitly).

    However, if we manually set the Restaurant property on the Image entity and save the validation error goes away.

     
    Two problems come to mind:
    * Image doesn't have a [Required] clause for Restaurant so it shouldn't balk at a null value. It balks
    * The validation error message is clearly wrong. Menu.Restaurant is set, but Image.Restaurant is not.

    In this business model the Image.Restaurant property CAN BE null - it's a valid option so explicitly setting the 
    property doesn't really solve this problem. That's why there is no [Required] clause on the property.

    It sort of looks like EF is getting confused on what it's validating. It looks to me like it's walking the object 
    graph and when it gets to Image.Restaurant it just looks for a Restaurant instance and uses the rules 
    from what it found higher up in the hierarchy rather than the current rules for that entity.

    Anybody have any idea why the validation error is occurring when Image.Restaurant isn't set and why the 
    error message displayed points at the [Required] error from the Menu object?


    +++ Rick ---

     


    Wednesday, September 21, 2011 5:35 AM

All replies