0

I am really really new to ASP.NET and I am dealing with a problem here. The problem is pretty much obvious in the title and I'm trying to solve it. Here is one of the list members I'm trying to add in the database:

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

This is the product class in the models folder:

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; }
    }

When I try to run the seed method with "Update-Database" in the Package Manager Console, I'm getting this error.

System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

And, this is the detail:

BabyStore.Models.Product failed validation - Price : The price must be a number up to two decimal places

Apparently it fails while checking the regex expression of "Price" but i don't really enter something that will fail this expression. As you see, the price is declared as "4.99m" which has 2 decimal places. What should I do to fix this? I'm Turkish so we're using comma instead of dot while seperating decimal places, so it might be the issue but i don't really know.

Any opinion is appreciated, thanks.

Furkan Yurdakul
  • 2,801
  • 1
  • 15
  • 37
  • that regex will be the bane of your existence. `Price` is double. So you are going to run into IEEE floating-point precision errors. Also, `double price = 4.90m;` when converted to a string to be validated by your regex may very well be converted to `4.9`. Personally I'd dump the regex and just round/truncate the value on the way into the database. – Sam Axe May 12 '18 at 18:07
  • Possible duplicate of [Best Data annotation for a Decimal(18,2)](https://stackoverflow.com/questions/19811180/best-data-annotation-for-a-decimal18-2) – CodeNotFound May 12 '18 at 18:11
  • I suggest you to use the Range instead of RegularExpression attribute. Because it more readable for any developper. – CodeNotFound May 12 '18 at 18:13
  • Price is in decimal, and as far as I know decimal and double are different from each other. The regular expression is required because it's used on visual side too. So how should I check the format of the input without regex? Is there a specific way to do it then? – Furkan Yurdakul May 12 '18 at 18:13
  • @CodeNotFound The answer on the possible duplicate one is not solved the issue, I'm still getting the error when I run "Update-Database" command. By the way, it already uses Range. – Furkan Yurdakul May 12 '18 at 18:17
  • 1
    Actually I managed to solve the issue by changing the regex to @"^\d+.?\d{0,2}$" so the database is filled. Thanks for your help @CodeNotFound – Furkan Yurdakul May 12 '18 at 18:28

0 Answers0