So I used this tutorial to generate my poco classes which I am to use throughout my aplication.. the problem is that Im not supposed to modify the generated cs files cause they get autoregenerated... How do I add attributes like [Required] and stuff like that?? please help
3 Answers
You can't add it directly (unless you modify T4 template to create them for you) but you can try to use trick introduced in ASP.NET dynamic data. All POCO classes are defined as partial. So lets define your partial part:
using System.ComponentModel.DataAnnotations;
[MetadataType(typeof(MyClassMetadata))]
public partial class MyClass
{
private class MyClassMetadata
{
[Required]
public object Id;
[Required]
[StringLength(100)]
public object Name;
}
}
Metadata class is special type to hold only metadata - it is never used. Name of fields must be same as corresponding fields in real class (field types doesn't matter so you can use object
).
Anyway in ASP.NET MVC you should use specialized View model for each view and pass data you need so the validation attributes will be placed in view model class.

- 360,892
- 59
- 660
- 670
-
3If you downvote it would be nice to let me know why? If my answer is incorrect I will modify or delete it ... – Ladislav Mrnka Feb 26 '11 at 17:13
-
1Advocating metadata buddy classes is advocating a bad practice. – John Farrell Feb 26 '11 at 17:17
-
4@jfar: Ok. Did you read last paragraph? :) I'm always trying to answer the question as well as provide the best practice if I don't agree with solution. – Ladislav Mrnka Feb 26 '11 at 17:22
-
I dont see how creating a ViewModel for absolutely each View in my app is a better approach than the buddy class to support the POCO class. – nacho10f Feb 26 '11 at 17:28
-
@Ladislav Mrnka - Yeah I saw that. I just auto downvote anything regarding metadatatype attributes. I think its harmful to the .net ecosystem. @NachoF - Your business/domain logic is coupled outward to the UI and the V. Your muddling concern separation. – John Farrell Feb 26 '11 at 17:40
-
2@jfar: You should go and downvote MS as well because EF Code First introduces DataAnnotations on POCO entities as common scenario: http://blogs.msdn.com/b/adonet/archive/2010/12/15/ef-feature-ctp5-validation.aspx Just kidding. I understand your concerns. – Ladislav Mrnka Feb 26 '11 at 17:54
-
2@jfar, why you so hate those DataAnnotation attributes? Sure, there is no place for them in the Entity classes but they work pretty well with ViewModels. – Vasiliy R Feb 26 '11 at 18:52
-
Thanks Ladislav Mrnka for posting, could you please point me out a tutorial or sample of code of the implementation with View Models?? Thanks in advance! – GibboK Jun 26 '12 at 09:52
-
@LadislavMrnka - My understanding is when you develop say a WCF based web app, its always better to have the validation at service level also as it can be consumed by other clients also. So, applying the same logic won't it be better to have validation at the model level, instead of at the view model as view model is restricted to the MVC Views while the entity model/domain model can be used by other apps? – Ramesh Apr 26 '14 at 14:22
The attributes on the generated POCOs are derived from the facets on the entities in the model. e.g. for [Required]
make sure the field is "not null" and for [StringLength(n)]
make sure the datatype is nvarchar(n)
via the MaxLength
facet.

- 9,748
- 1
- 31
- 28
-
Those attributes are generated by EF? or one must to find a way to add them to the POCO classes? – polkduran Jun 19 '13 at 15:58
-
If the facets are on the model, EF will add the appropriate attributes when it generates the POCOs. So to coerce a particular attribute, you need to know how to tweak the model. – Brian Kretzler Jul 31 '13 at 01:31
Further expanding on the answer. By using Microsoft Patterns & Practices Enterprise Library 5 Validation Block, you can open up a wealth of validation possibilities beyond those available through normal data annotations.
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
[HasSelfValidation]
public partial class Category : ICategory
{
[SelfValidation]
public void Validate(ValidationResults validationResults)
{
if (this.Title === "Credo")
{
validationResults.AddResult(
new ValidationResult(
"Category title cannot be a veiled reference to a former cool 2000AD character.",
this,
null,
null,
null));
}
validationResults.AddAllResults(
ValidationFactory
.CreateValidator<ICategory>()
.Validate(this));
}
}
using System;
using System.ComponentModel.DataAnnotations;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
public interface ICategory
{
int Id
{
get;
set;
}
[Required]
[StringLengthValidator(1, 50, MessageTemplate = "Category title should be a maximum of 50 characters in length.")]
string Title
{
get;
set;
}
}

- 5,810
- 6
- 29
- 36