简体   繁体   English

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

[英]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.

相关问题 如何在自动生成的视图中使ASP.NET MVC 3呈现具有复杂类型的对象? - How do I make ASP.NET MVC 3 render objects with complex types in the auto-generated views? 如何保持属性属性插入到ASP.NET MVC中自动生成的文件中 - How to keep properties attributes inserted into auto-generated files in ASP.NET MVC 将用户定向到 ASP.Net MVC 应用程序中的另一个页面会导致自动生成的代码出现语法错误 - Directing user to another page in ASP.Net MVC application leads to syntax error in auto-generated code ASP.Net自动生成的管理工具 - ASP.Net auto-generated Admin tool 在MVC自动生成的类中注释asp.net属性 - Annotate asp.net properties in MVC auto generated class ASP.NET MVC 5 中自动生成的 EDMX class 的构造函数 - Constructors for Auto Generated EDMX class in ASP.NET MVC 5 ASP.NET MVC 4项目的DBML文件 - DBML files for ASP.NET MVC 4 project ASP.net 4.5将当前自动生成的MachineKey复制到另一台服务器 - ASP.net 4.5 copy the current auto-generated MachineKey to another server 如何在asp.net MVC中自动生成的部分类中的View的iputs中设置属性的值 - How to set value of a property from iputs of View in auto generated partial class in asp.net MVC ASP.NET MVC3 System.ComponentModel.DataAnnotations——注释能否确定它正在验证的类? - ASP.NET MVC3 System.ComponentModel.DataAnnotations -- can an annotation determine the class it is validating?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM