2

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

Hasibul
  • 569
  • 2
  • 6
  • 21

2 Answers2

3

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.

Benjamin Gale
  • 12,977
  • 6
  • 62
  • 100
  • to make it clear, here MyEntity is the Database first Generated Class, [this](http://www.elevenwinds.com/data-validation-in-asp-net-mvc-database-first) too helped me – Shaiju T Feb 18 '15 at 12:48
0

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.

ADIMO
  • 1,107
  • 12
  • 24