4

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

2 Answers 2

7

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; }
}
Sign up to request clarification or add additional context in comments.

8 Comments

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.
ok, say i have a "Comment" type, how would i do it and is there a reason the classes are nested?
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.
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.
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)
|
1

A possible solution is http://linqtometadataaddin.codeplex.com :

Linq To MetaData AddIn is a Visual Studio 2010 tool that generate metadatatype class for dbml file. This add in is recomended for the Asp.net DynamicData applications

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.