开发者

Client Side validation in MVC 3 Database First

If I add [Required] in my entity class then unobtrusive validation works fine.

[Required] is not added where I generate my entity class using database first(*.edmx).

If I manually add [Required] in my ent开发者_运维百科ity property, something like

[Required]
public int {get;set;}

[Required] will delete when I update my edmx.

So my question is how can I perform client side validation if I use database first in EF.


Create a partial class for your entity and use the MetadataType attribute. See example below:

[MetadataType(typeof(MyEntity.Metadata))]
public partial class MyEntity
{
    private sealed class Metadata
    {
        [Required(ErrorMessage = "* required")]
        public string MyRequiredField { get; set; }
    }

    // Add other similar properties here...
}

This class will be unaffected by changes in the designer generated code.


This is a question for ado.net team. I suppose that when you use database first EF your domain model inherits constraints of database because database design is master for your application logic.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜