My class is marked as partial, but you cannot have partial properties. I thought I was on to something with the MetadataType class which is a new feature of Dynamic Data and DataAnnotations, but alas I feel it can only be used with Dynamic Data, is this true?

4 Answers
4

This is a known nuisance; you simply can't add metadata to the generated members.

There are 6 options here (in order of increasing effort):

if you own the attribute, make it possible to declare it against the class, for example: [ValidateNonEmpty("Name", "Name is required", ExecutionOrder = 1)] - then add multiple attributes to the partial class definition

use a virtual / interface / etc method to query this, rather than via attributes

use a custom TypeDescriptionProvider to provide dynamic metadata (lots and lots of work) - assuming that the consumer respects TypeDescriptor; most binding-related consumers do, but for example, Expression (used by many LINQ providers) doesn't

change the code-generator / write your own

try to extend something like PostSharp to do the work (I haven't found a way to do this, but I've love to hear if you find a way!)

I usually have success with the first option, unless it is a system-defined attribute ([DisplayName], etc). If [ValidateNonEmpty] is defined by dynamic data, then you might not be able to do this.

Thanks Marc, I thought this might be the case. I've managed to iterate through the properties of my "MetadataType" declared class, at the point I wished to enquire on the attributes, and I simply compare the name of the "meta" property against the real property.
–
GONealeJan 19 '09 at 6:14

It is not the same as inquisting the true attributes, I understand, but for what I need it looks like it will serve the purpose in this case. Which is great.
–
GONealeJan 19 '09 at 6:14

Hopefully this makes sense. I can now see if a validation attribute was declared and work accordingly. I now just hope there is no overhead with me utilising the MetadataType attribute class rather than making my own that simply tells it what class to look at properties on.
–
GONealeJan 19 '09 at 6:16

I think you would then need every single property from your base class also put into the interface, a tedious exercise if you have many generated classes and many properties.
–
secretwepOct 29 '14 at 17:42

This is a great solution, but it didn't work for my problem. I'm using EF 6 with code-first generated classes from an existing database. One of the columns in a table is an IDENTITY with auto generated values. However, the generated partial class did not provide the [DatabaseGenerated(DatabaseGeneratedOption.Identity)] attribute needed to have the database generate the key. The result is the error "Cannot insert explicit value for identity column in table 'mytable' when IDENTITY_INSERT is set to OFF.". I tried your solution but it didn't work. But if I add the attribute to the original generated class, it does work. So I'm still trying to find a solution that does not require the modifying of the auto generated file.

Another option is to wrap the properties inside non-generated properties in the same class. Not ideal because you may end up having double properties but if you can make your generator make protected properties it'd be a pretty good approach.

Just had to deal with this problem: Entity Framework generates classes, I want to serialize them to JSON with simpler names.