locked
EF 4.1 Validation Question RRS feed

  • Question

  • If I'm right when using a DDD Approach normally validation happens in the POCO-domain-entity-class in the Property Setters or you could use a Validate method in the same POCO-domain-entity class (eg Customer) to check all properties on validity.

    Would it give additional value when in Entity Framework 4.1 adding extra validation (besides the one in my POCO-classes) by using Data Annotation or Fluent API (and checking the result of the validation in the calling method using DbContext.GetValidationErrors() method). Or would this EF validation be redundant?

    eg.
    public CustomerMapping()
            {
                HasKey(x => x.Id);
                Property(x => x.LastName)
                    .HasColumnName("LastName")
                    .IsRequired()
                    .HasMaxLength(50);
                Property(x => x.FirstName)
                    .HasColumnName("FirstName")
                    .IsRequired()
                    .HasMaxLength(50);
                ...
            }

    Monday, March 21, 2011 11:21 AM

Answers

  • I see. In that case I think you would benefit from EF Validation, as it can verify if the value is specified at all (Required) has the proper length (MaxLength or StringLength) or matches a certain pattern (RegularExpressionAttribute).
    • Proposed as answer by Andriy Svyryd Wednesday, March 23, 2011 6:27 PM
    • Marked as answer by Alan_chen Monday, April 4, 2011 3:47 AM
    Wednesday, March 23, 2011 6:12 PM

All replies

  • Hi GuyD,

    Welcome!

    You can customize validation with overriding DbContext.ValidateEntity(), the more infromation about it, you can refer here:

    http://blogs.msdn.com/b/adonet/archive/2010/12/15/ef-feature-ctp5-validation.aspx

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, March 22, 2011 1:02 PM
  • To determine if EF will provide extra validation can you describe what validation is currently performed in your POCO-classes?

    Tuesday, March 22, 2011 11:35 PM
  • Thanks.

    As mentioned in my POCO classes normally I add validation to the setters of the properties or add a Validate method for validating purposes. Validation to me is only to check if a property has the correct value, is within the right boundaries, ... Pure business logic (eg calculating an Order Total) of course is something different that I implement as methods in my POCO's.

    Guy

    Wednesday, March 23, 2011 7:52 AM
  • I see. In that case I think you would benefit from EF Validation, as it can verify if the value is specified at all (Required) has the proper length (MaxLength or StringLength) or matches a certain pattern (RegularExpressionAttribute).
    • Proposed as answer by Andriy Svyryd Wednesday, March 23, 2011 6:27 PM
    • Marked as answer by Alan_chen Monday, April 4, 2011 3:47 AM
    Wednesday, March 23, 2011 6:12 PM