17

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 projects I've worked on have used attributes for most of the validation, but obviously this is not possible in the entity framework. Is the only way to do this by handling the partial methods in the property setters? All advice is much appreciated.

StuartLC
  • 104,537
  • 17
  • 209
  • 285
Micah
  • 111,873
  • 86
  • 233
  • 325

6 Answers6

11

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.

smaclell
  • 4,568
  • 7
  • 41
  • 49
  • How are #2 and #3 any different? The partial methods it generates are the OnChanged events ... unless I am missing something. – Jim Mitchener Nov 03 '09 at 22:53
  • You may be correct. It has been a long time since I have even looked at EF. – smaclell Nov 09 '09 at 00:59
  • 3
    I read #2 and #3 as entity- and property-level validation, respectively. These two strategies have different purposes, of course: entity-level validation tests the validity all of the properties together (eg. that mutually exclusive properties are not both set), whereas property-level validation considers only the content of a single property at a time (eg. that an email address is of the correct form). – ladenedge Feb 09 '10 at 19:52
6

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...

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
  • Oddly, the PEM extension and its samples are, for some reason, no longer available via MSDN. I'd love an updated link if anyone has one! – ladenedge Feb 09 '10 at 19:48
  • I hope they're not fooling us... I guess I was wrong saying out-the-box, I now realize that it's just an addon. It says that the addon can be downloaded from http://visualstudiogallery.msdn.microsoft.com/en-us/e6467914-d48d-4075-8885-ce5a0dcb744d but check out the link to see that it's broken. – Shimmy Weitzhandler Feb 09 '10 at 19:56
  • I found this link in the comments http://code.msdn.microsoft.com/DesignerExtStartKit/Wiki/View.aspx?title=Home&version=3, didn't have time to checkout yet. – Shimmy Weitzhandler Feb 09 '10 at 20:01
3

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.

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
aogan
  • 2,241
  • 1
  • 15
  • 24
1

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.

Rob
  • 1,663
  • 1
  • 19
  • 16
0

Consider implementing IValidatableObject in your entities.

Kamran
  • 782
  • 10
  • 35
0

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.

jbe
  • 6,976
  • 1
  • 43
  • 34