tags:

views:

21

answers:

1

Hello,

Let's say I've a partial class generated by Linq2Sql.Let's say the generated class has 2 properties LastName and FirstName. How do I add attributes to its properties using an other partial class?

Thanks for helping.

+1  A: 

Use metadata buddy classes

Ex:

[MetadataType(typeof(MyClassMetadata)]
public partial class MyClass
{
    public class MyClassMetadata
    {
         [StringLength(30)]
         public string FirstName {get;set;}

         [StringLength(30)]
         [Required]
         public string LastName {get;set;}    
    }
}
Gregoire
@Gregoire: Is that all it's there to do? Nice! But what's MyClassMetadata?
Richard77
@Richard77 : MyClassMetadata is the buddy class (see in the example) that is only the replication of the properties of your main class in order to assignate data annotation attributes
Gregoire
Should perhaps mention that this is specific to Linq-to-SQL; you can’t use this to add custom attribute to any field/property in any partial class.
Timwi
AnyWay, it works perfectly.
Richard77