Luis Deras
Luis Deras

Reputation: 1269

ASP.NET MVC Entity Framework: Data Annotations

I'm working with Entity Framework with a database-first approach. I already defined the model inside my application. Now I'm working with controllers and views. I used scaffolding in order to create controllers. Now I want to create rows.

Let's say I want to create employees, and let's say the DBA and EF made this possible:

    public partial class TBL_EMPLOYEE
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public TBL_EMPLOYEE()
        {
            this.TBL_EMPLOYEE = new HashSet<TBL_EMPLOYEE>();
        }

        public int EMPLOYEE_ID { get; set; }
        public String CO_WORKER_NUMBER { get; set; }
        public string NAME { get; set; }
        public string LAST_NAME { get; set; }
        public string SALARY { get; set; }
        public string PHONE_NUMBER { get; set; }
        public string EMAIL { get; set; }
        public string { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    }

Now, I need a view to create an employee, let's call this view VIEW 1 In this view, the user only needs to specify name and last name values. Both are required.

Now, in this VIEW 1 case I could use the following data annotations attributes in the same class, that'd be:

        [Required]
        public string NAME { get; set; }

        [Required]
        public string LAST_NAME { get; set; }

Now, let's go to the next case. I need another View, let's call this VIEW 2 In this one, the user needs to specify all values for all attributes. All of them are required except for name and last name.

THE REAL QUESTION

How can I use the same model class for both views? The example above here might seem a bit silly and trivial validations but I've been in bigger projects where entities are bigger and the idea of having different ViewModel classes is just so much work.

I've stumbled upon this in my .NET developments, to the point I had to create a ViewModel class per view in order to be specific with what the user needs to input and their validation. Is this the only way?

Upvotes: 0

Views: 375

Answers (2)

Khyron
Khyron

Reputation: 468

To avoid duplicating models with minor variations, try this:

https://stackoverflow.com/a/18898112/6850962

Basically, create a base model with data annotations that apply in all situations (eg: DisplayName) and then extend the model for variations (eg: Required attribute).

Upvotes: 1

Brian Mains
Brian Mains

Reputation: 50728

If you are trying to use your Entity Framework entities approach, I wouldn't put validation attributes on those entities. I would either:

  1. create separate distinct classes and then copy data from the EF entities to the models, and vice versa on update (either by writing the code explicitly, or using a tool like AutoMapper or many others). Then you can define the validation rules anyway you want. Unfortunately this approach does tightly couple validation to the model and thus model reuse may not be as possible.

  2. Use a more fluid validation framework like FluentValidation (https://github.com/JeremySkinner/FluentValidation). The benefit to this is that you define an external class with the rules internal, which can be applied differently depending on the situation. The model may still need some indicator on the model itself to figure out which rules apply, but this is another functional approach to handling the scenario you describe.

Upvotes: 0

Related Questions