从生成的类生成数据注释

发布于 2024-08-22 10:42:55 字数 1428 浏览 5 评论 0原文

我有一个 linq to sql 对象,或者如果需要的话实体框架对象。

我想为他们做 MVC 2 数据注释,但我无限懒。

有没有一种方法可以自动生成数据注释,

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

这样我就不必自己做这一切了?

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲世九天 2024-08-29 10:42:55

我认为生成数据注释是多余的。

相反,我建议编写一个关联的元数据提供程序,它只会使 MVC 模型绑定和验证看到您的类型的正确元数据,而根本不需要数据注释(或者将补充您可能已经拥有的任何数据注释)。

这里有一个示例

I think it would be redundant to generate data annotations.

Instead, I'd suggest writing an associated metadata provider which will simply cause the MVC model binding and validation to see the correct metadata for your types without requiring data annotations at all (or will supplement any data annotations you may already have).

There's an example here.

千里故人稀 2024-08-29 10:42:55

为此,我从 Silverlight 工具箱中借用了一些东西,但它似乎适用于 VS2010 中的 MVC3。

  1. 编译您的项目。如果您刚刚创建实体框架模型,这一点很重要。
  2. 右键单击您的项目。单击添加/新项目。
  3. 选择“域服务类”作为类型。单击添加。
  4. 在下拉列表中选择您的型号。
  5. 在实体列表中,选择您想要为其添加数据注释的所有对象。
  6. 选中标有“为元数据生成关联类”的框。单击“确定”。
  7. 您将生成两个类。只需删除没有 .metadata 的那个即可。标签。

应该可以做到这一点。您现在应该已经准备好元数据类来添加注释。 (上面使用的域服务类可能是与 VS2010 中的 WCF RIA 服务工具包一起安装的。这一点并不乐观,但如果您的可用项目列表中没有这个,那么这可能就是问题所在。)

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

  1. Compile your project. This is important if you just created your Entity Framework model.
  2. Right-click on your project. Click Add/New Item.
  3. Select 'Domain Service Class' as the type. Click Add.
  4. Select your model in the drop down.
  5. In the list of entities, select all objects the you want data annotations for.
  6. Check the box labeled 'Generate associated classes for metadata'. Click OK.
  7. You will get two classes generated. Just delete the one without the .metadata. tag.

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文