Generating Data Annotations from Generated Classes

蓝咒 提交于 2020-03-16 07:35:08

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/2330573/generating-data-annotations-from-generated-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!