将DataAnnontations添加到生成的局部类 [英] Adding DataAnnontations to Generated Partial Classes

查看:87
本文介绍了将DataAnnontations添加到生成的局部类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Subsonic3 Active Record生成的部分User类,该类已经用单独的部分类中的某些方法进行了扩展。

I have a Subsonic3 Active Record generated partial User class which I've extended on with some methods in a separate partial class.

我想知道它是否可以将数据注释添加到在另一个Subsonic上声明的一个子类的成员属性中。我尝试了此生成。

I would like to know if it is possible to add Data Annotations to the member properties on one partial class where it's declared on the other Subsonic Generated one I tried this.

public partial class User
{
    [DataType(DataType.EmailAddress, ErrorMessage = "Please enter an email address")]
    public string Email { get; set; }

    ...
}

该示例给出了成员已定义错误。

That examples gives the "Member is already defined" error.

我想我之前可能已经看过一个示例,该示例说明我要如何处理Dynamic Data和Linq2Sql。

I think I might have seen an example a while ago of what I'm trying to do with Dynamic Data and Linq2Sql.

推荐答案

您需要做的是创建一个'伙伴类'并将数据注释应用于该类:

What you will need to do is create a 'buddy class' and apply the Data Annotations to that class:

[MetadataType(typeof(UserValidation))]
public partial class User 
{
  ...
}

public class UserValidation
{
  [DataType(DataType.EmailAddress, ErrorMessage = "Please enter an email address")]
  public string Email { get; set; }
}

这篇关于将DataAnnontations添加到生成的局部类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆