0

If I put [Required] attribue over a property in a model then the <input> helper tag will throw out a validation error if no value is provided, but also it will be affecting database structure.

However does this

   modelBuilder.Entity<CartItem>(e =>
        {
            e.Property(e=>e.Quantity).IsRequired();
        }

do the same? Meaning is this causing somehow dynamically adding [Required] attribute to a property so <input> tag helper can notice it?

Yoda
  • 17,363
  • 67
  • 204
  • 344

1 Answers1

0

"Meaning is this causing somehow dynamically adding [Required] attribute to a property so tag helper can notice it?"

Yes, exactly it is. IsRequired and the [Required] will eventually have the same impact on database schema as it will add a non-nullable constrain on the table column. Means the property would be mandatory. The key difference is we only can use fluent API validation code first's approach while [Required] all approach.

In fact, while using e.Property(e=>e.Quantity).IsRequired() attribute fluent API instead of annotations to get the same client side & server side validation. Rather than use Required. The thing is validation errors thrown based on the Fluent API configurations will not automatically reach the UI, but you can capture it in code and then respond to it accordingly.In addition, you can check the official document here

Md Farid Uddin Kiron
  • 16,817
  • 3
  • 17
  • 43