开发者

Can I add MVC's Required Attribute to a class defined elsewhere by inheriting the class?

I have a bunch o开发者_StackOverflow中文版f classes generated from my EntityFramework based DAL. I don't want to modify those classes directly in case I regenerate them from the database, but I want to be able to specify certain fields are [Required]. I thought about making another class that inherited from my data object and add the attribute to the new class's field, but then I have to hide the base class's property. Is there a good/easy way to do this without worrying about losing my manual changes on regeneration, and without rewriting the objects entirely?

Thanks


you can define your models as partial classes and specify metadata else where.

ex:

[MetadataType(typeof(UserMetaData))]
public partial class User
{
    public string Name { get; set; }
}

public class UserMetaData
{
    [Required]
    public string Name;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜