0

我对 ASP.NET 真的很陌生,我正在处理一个问题。这个问题在标题中非常明显,我正在努力解决它。这是我要在数据库中添加的列表成员之一:

new Product 
{
    Name = "Sleep Suit", 
    Description="For sleeping or general wear",
    Price=4.99m, 
    CategoryID=categories.Single( c => c.Name == "Clothes").ID 
}

这是模型文件夹中的产品类:

public partial class Product
    {
        public int ID { get; set; }

        [Required(ErrorMessage = "The product name cannot be blank.")]
        [StringLength(50, MinimumLength = 3, ErrorMessage = "Please enter a product name between 3 and 50 characters in length.")]
        [RegularExpression(@"^[a-zA-Z0-9'-'\s]*$", ErrorMessage = "Please enter a product name made up of letters and numbers only.")]
        public string Name { get; set; }

        [Required(ErrorMessage = "The product description cannot be blank.")]
        [StringLength(200, MinimumLength = 10, ErrorMessage = "Please enter a product description between 10 and 200 characters in length.")]
        [RegularExpression(@"^[,;a-zA-Z0-9'-'\s]*$", ErrorMessage = "Please enter a product description made up of letters and numbers only.")]
        [DataType(DataType.MultilineText)]
        public string Description { get; set; }

        [Required(ErrorMessage = "The price cannot be blank.")]
        [Range(0.10, 10000, ErrorMessage = "Please enter a price between 0.10 and 10000.00")]
        [DataType(DataType.Currency)]
        [DisplayFormat(DataFormatString = "{0:c}")]
        [RegularExpression("^[0-9]+(\\.[0-9][0-9])?$", ErrorMessage = "The price must be a number up to two decimal places")]
        public decimal Price { get; set; }

        public int? CategoryID { get; set; }

        public virtual Category Category { get; set; }

        public virtual ICollection<ProductImageMapping> ProductImageMappings { get; set; }
    }

当我尝试在包管理器控制台中使用“更新数据库”运行种子方法时,我收到了这个错误。

System.Data.Entity.Validation.DbEntityValidationException:一个或多个实体的验证失败。有关更多详细信息,请参阅“EntityValidationErrors”属性。

而且,这是细节:

BabyStore.Models.Product 验证失败 - 价格:价格必须是最多两位小数的数字

显然它在检查“价格”的正则表达式时失败了,但我并没有真正输入会使这个表达式失败的东西。如您所见,价格声明为“4.99m”,有 2 位小数。我应该怎么做才能解决这个问题?我是土耳其人,所以我们在分隔小数位时使用逗号而不是点,所以这可能是问题,但我真的不知道。

任何意见表示赞赏,谢谢。

4

0 回答 0