Question

OK, so I still want to use the schema model from EF 4.0, but I also want the API and flexibility of EF 4.1.

So, in my application, I added DbContext generation code from my current EF 4.0 models.

Now, my questions is what are my options for validating my DbSet (EF 4.1) models without them getting wiped out?

DataAnnotation doesn't seem to cut it because everytime I update my visual model, it COMPLETE WIPES out all my modifications.

  • I have heard about partial classes, but that seems to be really messy.
  • I don't really want to do validations on my controller.
  • I want to make this scalable so I can easily transfer my classes from asp.net MVC to WPF and etc.

Is there a way to do this? Validation repository? Seperate validation layer? Validation inside the repository layers? Examples would be appreciated too.

Please help me. Thanks :)

Was it helpful?

Solution

using a template generator, it seems possible to do one of the following:

  1. Create a buddy class for each of your entities, and define the buddy class by naming convention in the template.
  2. Figure a way to gen the data annotations, by storing them in a separate source or something.

Or, use a separate layer. If you are using repositories, you could embed validation there. I chose to have an Orm Validation factory, which pulls in rules matching an entity (either statically or dynamically), and the factory does the work (since that's more along the lines of the singular responsibility principle).

HTH.

OTHER TIPS

In my project I mix both 4.1 and 4.0 contexts. It is possible to do a slow migration. There is also a entity framework 4.1 reverse engineer extension that can be added to visual studio. (to let you use 4.1 by code generating poco objects + mappings from your existing database).

I wouldn't recommend creating a DbContext that exposes edmx generated models from 4.0. You also loose the benefits of inheriting objectcontext/dbcontext , inheriting properties (row versioning, ect), and controlling the poco object (non code generated so you don't have to worry about partials + metadatatype attribute as in 4.0)

Your question related to validating schema created with ef4.0:

So that data annotations are NOT completely wiped when updating your model you need to use MetadataType attribute.

http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.metadatatypeattribute.aspx

which has an example

I don't know why MS teaches programmers such bad practices. Especially using data annotations for mapping in EF 4.1 was really stupid design decision (for example Linq-to-SQL used separate attributes set to make clear separation between mapping and validation).

Data annotations are used to validate data from user input and user input is view / controller related stuff where each view can need different validation rules => if you want to use data annotations these rules belongs to view model not to entity.

In the simplest cases where view and entity is 1:1 you can use data annotations directly but that is really for simplest applications and once you do such a simple application you can most probably use ASP.NET Dynamic data directly and let framework create all pages for you based on your entity model.

ASP.NET Dynamic data also shows how to apply data annotations for generated entities in buddy classes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top