从生成的类生成数据注释 [英] Generating Data Annotations from Generated Classes

查看:174
本文介绍了从生成的类生成数据注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个LINQ to SQL的对象,如果neccessary实体框架对象。

I have a linq to sql object or if neccessary Entity Framework object.

我想要做的MVC 2数据注释他们,但我不停地懒。

I want to do MVC 2 Data Annotations for them, but I am endlessly lazy.

有没有办法来自动生成数据注释的香格里拉

Is there a way to automatically generate the data annotations a-la

[Bind(Include = "Title,Description,EventDate,Address,Country,ContactPhone,Latitude,Longitude")]
[MetadataType(typeof(Dinner_Validation))]
public partial class Dinner
{
    public bool IsHostedBy(string userName)
    {
        return HostedBy.Equals(userName, StringComparison.InvariantCultureIgnoreCase);
    }

    public bool IsUserRegistered(string userName)
    {
        return RSVPs.Any(r => r.AttendeeName.Equals(userName,     StringComparison.InvariantCultureIgnoreCase));
    }
}

public class Dinner_Validation
{
    [Required(ErrorMessage = "Title is required")]
    [StringLength(50, ErrorMessage = "Title may not be longer than 50 characters")]
    public string Title { get; set; }

    [Required(ErrorMessage = "Description is required")]
    [StringLength(265, ErrorMessage = "Description may not be longer than 256 characters")]
    public string Description { get; set; }

    [Required(ErrorMessage = "HostedBy is required")]
    public string HostedBy { get; set; }

    [Required(ErrorMessage = "Address is required")]
    public string Address { get; set; }

    [Required(ErrorMessage = "Country is required")]
    public string Country { get; set; }

    [Required(ErrorMessage = "Phone# is required")]
    public string ContactPhone { get; set; }
}

所以,我没有做这一切我自己?

So that I don't have to do it all myself?

推荐答案

我借一点点从我这的Silverlight工具箱,但似乎在VS2010的工作只是罚款MVC3。

I borrowed a little from my Silverlight toolbox for this, but it seems to work just fine for MVC3 in VS2010.


  1. 编译您的项目。如果你只是创建了实体框架模型,这是非常重要的。

  2. 在您的项目中单击鼠标右键。单击添加/新项目。

  3. 选择域服务类作为类型。单击添加。

  4. 选择在下拉菜单中你的模型了。

  5. 在实体列表,选择所有对象的你想要的数据注解。

  6. 检查标记生成相关的类元数据复选框。点击OK。

  7. 您将获得生成的两班。只是删除无.metadata之一。标签。

这应该这样做。你现在应该有一个元数据类准备添加您的注释。 (这有可能是域名服务类中使用上面在VS2010中WCF RIA服务工具包安装。不积极有关,但是如果你没有这个在可用的产品清单做,这可能是这个问题。)

That should do it. You should now have a metadata class ready to add your annotations. (It's possible that the Domain Service Class used above was installed with the WCF RIA Services toolkit in VS2010. Not positive about that, but if you don't have this in your list of available items, that's probably the issue.)

这篇关于从生成的类生成数据注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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