in

DĂ© specialist in .NET trainingen en consultancy

Jo-wen Mei

MetadataType attribute

I like the usage of attributes, mostly because it keeps my code clean. At the moment, I'm working with the Entity Framework, so my entities are generated. I want to use attributes for validation, and configure scaffolding (for DynamicData).

But how can you place an attribute on a property of a generated class??

If you put it in the generated class, it will be lost when you regenerate, obviously.

Normally, you put the custom logic in the partial class, but it's just impossible to redefine properties as they're not partial.

This is a common scenario, so there must be a solution to this: MetadataType attribute!

image

So the trick is: on the generated class, refer to a metadata class using the MetadataType attribute. Here, you can put all the attributes you want!

(note: no instances of the metadata class are made)

 

more info

dynamic metadata

Comments

No Comments