[英]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). 我担心这样做,因为我的更改将在dbml文件再次自动生成时被覆盖(通常是这样)。
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... ... ...我的dbml文件列表中的可扩展箭头丢失了,右键单击并选择“查看代码”只显示一个空的分部类,如下所示...
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")]
我正在使用VS2010 RC并且我正在开发一个MVC 2.0应用程序,我希望能够使用UI注释,例如
[UIHint("RelativeDateTime")]
edit: 编辑:
problem solved, thanks steve, here is my VB version edit as an example... 问题解决了,谢谢史蒂夫,这里以我的VB版编辑为例......
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. 您可以使用DataAnnotations的“伙伴类”功能来定义类型的验证。 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 : 一个可能的解决方案是http://linqtometadataaddin.codeplex.com :
Linq To MetaData AddIn is a Visual Studio 2010 tool that generate metadatatype class for dbml file.
Linq To MetaData AddIn是一个Visual Studio 2010工具,可为dbml文件生成元数据类型。 This add in is recomended for the Asp.net DynamicData applications
对于Asp.net DynamicData应用程序,建议使用此添加项
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.