Peet Brits
Peet Brits

Reputation: 3247

ASP.NET MVC and Entity Framework: Generated Data Validation

I know it is possible to manually add attributes to entity properties, but can the entity framework generate this for me? For example, it knows the maximum length is 6 (if you look at the properties in the entity designer).

What happens now is that I get a nasty generic error message with no indication of the field's length.

I suppose I need training in Entity Framework for MVC which I should squeeze in somewhere between deadlines. :-)

Upvotes: 0

Views: 455

Answers (2)

Margus
Margus

Reputation: 20028

Ways to create:

  • Database first
    • existing database -> generated data model
  • Model first :
    • data model -> generated database
  • Code first :
    • existing database -> generated data model
    • data model -> generated database

In case of "Database first" this should be constraint. Checks happen in database.

CREATE TABLE suppliers
(
  supplier_id numeric(4),
  supplier_name varchar2(50),
  CONSTRAINT check_supplier_id
  CHECK (supplier_id BETWEEN 100 and 9999)
);

Example : source

In case of model first you do this with annotations or validation classes. Example Fluent valitation

public class CustomerValidator: AbstractValidator<Customer> {
  public CustomerValidator() {
    RuleFor(customer => customer.Surname).NotEmpty();
    RuleFor(customer => customer.Forename).NotEmpty().WithMessage("Please specify a first name");
    RuleFor(customer => customer.Discount).NotEqual(0).When(customer => customer.HasDiscount);
    RuleFor(customer => customer.Address).Length(20, 250);
    RuleFor(customer => customer.Postcode).Must(BeAValidPostcode).WithMessage("Please specify a valid postcode");
  }

  private bool BeAValidPostcode(string postcode) {
    // custom postcode validating logic goes here
  }
}

Example : source

In case of code first you can add property constraints:

  protected override void OnModelCreating(DbModelBuilder modelBuilder) 
  { 
      modelBuilder.Entity<Blog>().Property(p => p.BloggerName).HasMaxLength(10); 
  } 

Example : source

Upvotes: 1

L-Four
L-Four

Reputation: 13531

You could probably achieve this by creating your own T4 templates.

However, validation is something I won't do in your data layer alone, but especially in your business layer. After all, it's not only the maximum length of a field that is important, but also whether it's mandatory or not, a number within specific ranges, validations that are dependent on other entities, and so on. Not every validation/business rule can be auto-generated, as it mostly also depends on context. Depending on your architecture you can for example don't allow the creation of an object if requirements are not fulfilled.

A nice framework for doing this kind of validation is FluentValidation. It allows you to isolate the validations in separate classes; and even reuse them.

Your data layer could be responsible for data validation like checking foreign key or other constraints.

Upvotes: 1

Related Questions