代码之家  ›  专栏  ›  技术社区  ›  ProfK

确定EF实体中ConstraintException的源

  •  0
  • ProfK  · 技术社区  · 15 年前

    我有一个实体类,它具有一组不可为空的字符串属性。如果我尝试将其中一个设置为空,我会得到一个 约束异常 以及信息“ 此属性不能设置为空值。

    如果不在赋值之前将tracker变量设置为每个属性的名称,有没有一种方法可以确定哪个属性赋值导致了异常被抛出?

    2 回复  |  直到 12 年前
        1
  •  1
  •   marvelTracker    14 年前

    在poco类的一侧,可以使用checker模式进行验证。对不可为空的属性使用构造函数注入。

    public class Product:Entity<long>
    {
        public Product(string description, string productNo, double? unitPrice, Category category)
        {           
    
           Check.Require(unitPrice.HasValue, "Unit price can not be null in Product");
           Check.Require(!string.IsNullOrEmpty(description),
                  "Description can not be null in Product");            
    
            Check.Require(!string.IsNullOrEmpty(productNo), 
               "Product number can not be null in Product");
    
            Check.Require(category != null, "Category can not be null in Product");
    
            UnitPrice = unitPrice.Value;
            Description = description;
            ProductNumber = productNo;
            Category = category;
        }  
    
    
    
        /// <summary>
        /// Gets or sets the product id.
        /// </summary>
        /// <value>
        /// The product id.
        /// </value>
        public int ProductId { get; set; }
    
        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        /// <value>
        /// The name.
        /// </value>
        public string Name { get; set; }
    
        /// <summary>
        /// Gets or sets the category id.
        /// </summary>
        /// <value>
        /// The category id.
        /// </value>
        public long CategoryId { get; set; }
    
        /// <summary>
        /// Gets or sets the category.
        /// </summary>
        /// <value>
        /// The category.
        /// </value>
        public Category Category { get; set; }
    
        /// <summary>
        /// Gets or sets the unit price.
        /// </summary>
        /// <value>
        /// The unit price.
        /// </value>
        public double UnitPrice
        {
            get; 
            set;
        }
    
        /// <summary>
        /// Gets or sets the description.
        /// </summary>
        /// <value>
        /// The description.
        /// </value>
        public String Description
        {
            get;
            set;
        }
    
        /// <summary>
        /// Gets or sets the product number.
        /// </summary>
        /// <value>
        /// The product number.
        /// </value>
        public string ProductNumber
        {
            get; 
            set;
        }
    }
    

    }

    public class Check
    {
           /// <summary>
        /// Requires the specified assertion.
        /// </summary>
        /// <param name="assertion">if set to <c>true</c> [assertion].</param>
        /// <param name="message">The message.</param>
        public static void Require(bool assertion, string message)
        {
            if(!assertion)
            {
                throw new PreConditionException(message);
            }
        }
    
        /// <summary>
        /// Ensures the specified assertion.
        /// </summary>
        /// <param name="assertion">if set to <c>true</c> [assertion].</param>
        /// <param name="message">The message.</param>
        public static void Ensure(bool assertion, string message)
        {
            if (!assertion)
            {
                throw new PostConditionException(message);
            }
        }
    }
    
    /// <summary>
    /// Exception raised when a precondition fails.
    /// </summary>
    public class PreConditionException : Exception
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="PreConditionException"/> class.
        /// </summary>
        /// <param name="message">The message.</param>
        public PreConditionException(string message):base(message)
        {
    
        }
    }
    
    /// <summary>
    /// Exception raised when a postcondition fails.
    /// </summary>
    public class PostConditionException:Exception
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="PostConditionException"/> class.
        /// </summary>
        /// <param name="message">The message.</param>
        public PostConditionException(string message):base(message)
        {
    
        }
    }
    
        2
  •  0
  •   alun    14 年前

    你能进入StackTrace吗?它将具有类似“myEntity.set_myNotNullableProperty42”的内容,该内容告诉您将哪个属性设置为空。