Search code examples
model-view-controllerentitiesctp4

How do I use Entities CTP4 model first and then add data annotations such as validation rules?


I'm creating a model, which is then generating SQL to create the database. Now I have some great entity classes in a single .designer.cs file.

However, I then want to add [Required] to some of the fields that I've created model-first. I've created public partial classes, but I can't redefine the fields to add the [Required] annotation.

Any thoughts?


Solution

  • As far as I can tell so far, with CTP5, this hasn't really changed from plain EF 4; you create partial classes for the entities you need to validate, then use the MetadataTypeAttribute. This is an obnoxious way to do things, but you can read all about it on MSDN here.

    CTP 5 also added a T4 generation template that lets you use the model to generate DbContext based classes instead of the more traditional ones from plain EF 4. This is detailed here.. This doesn't change, as far as I can see, the need to use partials and MetadataTypeAttributes.

    I'm still hoping for some convergence here, but so far most of the real goodies in the CTPs seem to be going to the Code-First camp which can now use data annotations without any acrobatics. Of course, finding solid info on the CTPs is also a bit hard. The docs there, and the web is polluted with so much noise from previous CTPs that good info is getting very hard to find.