添加的DataAnnotations来自动生成DBML类? MVC 2.0 ASP.NET自动生成、DBML、DataAnnotations、MVC

2023-09-03 05:22:45 作者:等尽歌悲欢

我很担心这样做,因为当DBML文件是自动再次生成(因为他们常常是)我的更改都会被覆盖。

我在想这样做的部分类,并写出相同的属性来注释他们,但担心它会抱怨重复,我可以的原因甚至没有实验使我想到我的问题的第二部分。 ..

...这,我的dbml文件列表的扩展箭头丢失,右击并选择View code只是显示一个空的部分类,如下...

 部分类FPDataContext
末级
 

所以,我甚至不能查看类!任何人任何想法重新出现这些问题?

我用VS2010 RC和我只是开发一个MVC 2.0应用程序,在这里我希望能够使用UI注释,比如 [UIHint(RelativeDateTime)]

编辑:

问题解决了,感谢史蒂夫,这是我的VB版的编辑,例如,...

 导入System.ComponentModel.DataAnnotations

< MetadataType(的GetType(CommentMetaData))> _
部分公共类注释
末级

公共类CommentMetaData
    < UIHint(PostedSince)> _
    公共财产DateAdded()为DATETIME

末级
 

解决方案 Datafactory数据生成怎么连接mysql数据库

您可以使用的DataAnnotations的伙伴类的功能,在你的类型定义的验证。这基本上意味着你在其他类中定义的验证,但你也可以定义这个类内部您现有的类:

  [MetadataType(typeof运算(CommentMetaData))]
公共部分类评价{
}

公共类CommentMetaData {
    [StringLength(50),必需]
    公共对象名称{;组; }
    [StringLength(15)]
    公共对象颜色{获得;组; }
    [范围(0,9999)
    公共对象体重{获得;组; }
}
 

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

解决方案

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