开发者

Adding DataAnnotations to auto-generated DBML Class? MVC 2.0 ASP.NET

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 e开发者_StackOverflow中文版ven 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


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; }
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜