locked
Validate changing foreign key RRS feed

  • Question

  • User1105011734 posted

    I am using the default dynamic data SP1 project template with a single linq-to-sql table that contains a foreign key to itself (to establish a parent-child relationship). Everything works fine out of the box until I attempt to add logic to the foreign key.  I added a method like the one below and instead of seeing the error message in the Edit template page, the exception is unhandled and shown to the user on the yellow error page (partial rendering is disabled for debugging).  As a test I added validation logic to a text field and it worked as expected.  I added a DynamicValidator to the ForeignKey_Edit.asx file and added SetUpValidator() to the Load() but it's still not catching the exception.  What else needs to be done to catch these exceptions and display them properly?

     

    // Prevent setting parent to itself
    partial void OnParentIdChanging(int? value)
    {
      if(value.HasValue && value.Value == Id)
      {
        throw new ValidationException("Entity may not be its own parent.");
      }
    }
      
    Wednesday, November 5, 2008 8:56 PM

Answers

  • User-1005219520 posted

    The correct way to check the parent and the child keys is in the OnValidate method. The following code works:

    DisplayName("Product Cats")]
    public partial class ProductCategory {
    
        partial void OnValidate(System.Data.Linq.ChangeAction action) {
            doValidation();
        }
    
        void doValidation() {
            if (ProductCategoryID == ParentProductCategoryID)
                throw new ValidationException("Entity may not be its own parent.");
        }
    
        partial void OnNameChanging(string value) {
    
            if (String.Compare(value, "Joe", true) == 0)
                throw new ValidationException("Joe is not allowed.");
        }
    }
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 6, 2008 1:15 PM

All replies

  • User-1005219520 posted

    Hi mdaubert,

    Thanks for bringing this to our attention. I have reproduced the problem.  I'll get back to you as soon as I figure out a work around.

    Wednesday, November 5, 2008 10:33 PM
  • User-1005219520 posted

    Hi mdaubert,

    It looks like the comparison is failing because on edit or insert, a new entity is created. The ID  property is 0 at that time so the comparison looks OK. I'm getting closer  by adding the OnParentProductCategoryIDChanged() method ( I'm using AdventureWorksLT and the ProductCategory entity ) - My validator is not catching the error - but I am catching the error on the client side and reporting the error.

    Here is the code that is closer:

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.DynamicData;
    using System.ComponentModel.DataAnnotations;
    
    using System.ComponentModel;
    using System.Text.RegularExpressions;
    
    [DisplayName("Product Cats")]
    [MetadataType(typeof(ProductCategoryMD))]
    public partial class ProductCategory {
    
        partial void OnValidate(System.Data.Linq.ChangeAction action) {
            doValidation();
        }
    
        void doValidation() {
            if (ProductCategoryID == ParentProductCategoryID)
                throw new ValidationException("Entity may not be its own parent.");
        }
    
        partial void OnParentProductCategoryIDChanged() {
            doValidation();
        }
    
        partial void OnProductCategoryIDChanged() {
            doValidation();
        }
    
        partial void OnNameChanging(string value) {
    
            if (String.Compare(value, "Joe", true) == 0)
                throw new ValidationException("Joe is not allowed.");
        }
        public class ProductCategoryMD {
    
        }
    }
     

     And the error message I get:

    Failed to set one or more properties on type ProductCategory.  Entity may not be its own parent.

    Description: An unhandled exception occurred

     

    Wednesday, November 5, 2008 11:02 PM
  • User-1005219520 posted

    The correct way to check the parent and the child keys is in the OnValidate method. The following code works:

    DisplayName("Product Cats")]
    public partial class ProductCategory {
    
        partial void OnValidate(System.Data.Linq.ChangeAction action) {
            doValidation();
        }
    
        void doValidation() {
            if (ProductCategoryID == ParentProductCategoryID)
                throw new ValidationException("Entity may not be its own parent.");
        }
    
        partial void OnNameChanging(string value) {
    
            if (String.Compare(value, "Joe", true) == 0)
                throw new ValidationException("Joe is not allowed.");
        }
    }
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 6, 2008 1:15 PM
  • User1105011734 posted

    Thank you very much for checking this out.  Using OnValidate is okay but I was hoping to be able to drop the validation error message next to the field that caused the error, which is why I was trying to use OnProductCategoryIDChanging().  I can see why this wouldn't work now.  I'm surprised there's no simple solution here to get the validation text in the correct location without sloppy code.

    Friday, November 7, 2008 11:09 AM