views:

295

answers:

1

i'm worried about doing this since my changes will be overwritten when the dbml file is auto generated again (as they often are).

i'm thinking of doing a partial class and writing out the same properties to annotate them, but worried that it will complain about duplicates, and the reason i can't even experiment brings me to the second part of my questions...

... that, the expandable arrow on my dbml file listing is missing, right clicking and selecting "View Code" just displays an empty partial class as below...

Partial Class FPDataContext
End Class

So, i can't even view the class! anyone any ideas re any of these problems?

I'm using VS2010 RC and am just developing an MVC 2.0 app where i want to be able to use the UI Annotations such as [UIHint("RelativeDateTime")]

edit:

problem solved, thanks steve, here is my VB version edit as an example...

Imports System.ComponentModel.DataAnnotations

<MetadataType(GetType(CommentMetaData))> _
Partial Public Class Comment
End Class

Public Class CommentMetaData
    <UIHint("PostedSince")> _
    Public Property DateAdded() As DateTime

End Class
+5  A: 

You can use the 'buddy class' feature of DataAnnotations to define validations on your type. This basically means that you define the validations on another class, but you can also define this class 'inside' your existing class:

[MetadataType(typeof(CommentMetaData))]
public partial class Comment {
}

public class CommentMetaData {
    [StringLength(50),Required]
    public object Name { get; set; }
    [StringLength(15)]
    public object Color { get; set; }
    [Range(0, 9999)]
    public object Weight { get; set; }
}
Steven
Beat me to it, +1
Kirschstein
could you tell me what ProductMD refers to? i'm assuming this is the dbml class that im trying to annotate? thanks mate this is a great help, esp since i don't need to interact with or touch the auto generated class.
Erx_VB.NExT.Coder
ok, say i have a "Comment" type, how would i do it and is there a reason the classes are nested?
Erx_VB.NExT.Coder
Nesting of the class is optional, but this keeps your entity and it's metadata close together. You can easily define the ProductMD on the same level as the Product class. This nested class is not added to the auto generated part of the entity, but in the manual part.
Steven
I updated the code. Here you have a "Comment" type and now I defined the "CommentMetaData" on the same level (so not nested anymore) as the Comment entity.
Steven
does this actually extend the existing class or does it create a different class with it's own memory allocation etc (talking about the class with the properties in it)
Erx_VB.NExT.Coder
im getting it now, but do i need to place "[MetadataType(typeof(CommentMetaData))]" on top of the ORIGINAL class? because i dont have access to the original class. thanks.
Erx_VB.NExT.Coder
You need this on top of the original class, but this will not be a problem. You can create a different file in your project and place their your second 'public partial class Comment'. C# will compile this to one single class. That's the beauty of partial classes :-)
Steven
thanks steve, i've gotten it working, and have posted a VB edit on the questions.
Erx_VB.NExT.Coder