Can I define properties in partial classes, then mark them with attributes in another partial class?

Viewed 40350

Is there a way I can have a generated code file like so:

public partial class A 
{
    public string a { get; set; }
}

and then in another file:

public partial class A 
{
    [Attribute("etc")]
    public string a { get; set; }
}

So that I can have a class generated from the database and then use a non-generated file to mark it up?

5 Answers

You need to define a partial class for your A class just like below example

using System.ComponentModel.DataAnnotations;

// your auto-generated partial class
public partial class A 
{
    public string MyProp { get; set; }
}

[MetadataType(typeof(AMetaData))]
public partial class A 
{

}

public class AMetaData
{
    [System.ComponentModel.DefaultValue(0)]
    public string MyProp { get; set; }
}
Related