Entity Framework Validation

前端 未结 6 1538
情话喂你
情话喂你 2020-12-14 09:58

I\'m getting ready to start a new project and I\'ve been researching the entity framework. My question is what is the best strategy for validating the entities? Other projec

相关标签:
6条回答
  • 2020-12-14 10:21

    In .NET 4, there is going to be out-the-box validation support in Entity-Framework.

    Check out: http://blogs.msdn.com/adonet/archive/2010/01/13/introducing-the-portable-extensible-metadata.aspx

    So don't work to hard on implementing too complex validation logic...

    0 讨论(0)
  • 2020-12-14 10:21

    We have overrident the object context and intercept the SaveChanges() method

    public abstract class ValidationObjectContext : ObjectContext{
        ...
    
        public override int SaveChanges(SaveOptions options){
            ValidateEntities();
            return base.SaveChanges(options);
        }
    
    }
    

    That way the validation is left until the last minute before the connections are made but after you are (expecting) to be happy with the graph and ready to commit, (as opposed to other options to validation on any change, since some complex rules like those we have are only valid after several properties are set.). We have two levels of validation, Basic Property validation, things like string length, nullability etc. And Business Logic validation, which might require checking rules across multiple objects, possibly hitting the database to confirm.

    0 讨论(0)
  • 2020-12-14 10:24

    Consider implementing IValidatableObject in your entities.

    0 讨论(0)
  • 2020-12-14 10:26

    If you are using WPF or Windows Forms then you might implement the IDataErrorInfo interface.

    The BookLibrary sample application of the WPF Application Framework (WAF) project shows how entities created by the Entity Framework can be validated.

    0 讨论(0)
  • 2020-12-14 10:39

    If you use ASP.NET MVC, then you could use Validation Application Block or the System.ComponentModel.DataAnnotations. The articles Using Data Annotations and Using Application Block show how to do them using Linq, but the usage with entity-framework should be similiar.

    0 讨论(0)
  • 2020-12-14 10:40

    I have not actually used the Entity framework before but a quick search indicates that you have several options.

    1) Validate at another layer in your application

    Always an option, I just thought I would throw it out there explicitly.

    2) Hook into the OnChanged events of the Entity then perform validation

    Likely brittle and would become confusing/slow after if you have many different properties things that can change for each entity.

    3) Implement partial methods to validate property changes

    According to this post and this walkthrough there are partial methods available for validation. This seems like your best option as it is not very intrusive and you can selectively implement the validation you want.

    I hope that helps. Good luck.

    0 讨论(0)
提交回复
热议问题