myfinite
myfinite

Reputation: 89

using entity framework code first in mvc with view model?

so when implementing entity framework code first in mvc, do we separate the view restrictions from view model? this is because for database first the model is generated(so i see the reason to separate it to view model but how about code first?)

The next questions i would ask is it ok to separate view model to another folder? since by default asp.net is MVC there is no view model inside

Model <--- what is this model call? data model? domain model? business model?

 public class Student
    {
        public int ID { get; set; }
        [StringLength(250)]
        public string LastName { get; set; }
        public string FirstMidName { get; set; }
        public DateTime EnrollmentDate { get; set; }
    }

View Model

public class Student
    {
        public int ID { get; set; }
        [MaxLength(250)]
        [Required]
        public string LastName { get; set; }
        [Required]
        public string FirstMidName { get; set; }
        [Required]
        public DateTime EnrollmentDate { get; set; }
    }

Upvotes: 0

Views: 1298

Answers (2)

M.Azad
M.Azad

Reputation: 3763

Your model that Used in mvc views is viewmodel.
your model that persist in database is domain model.

Your domain model may has some properties that you don't need use it in your client.
Your Service layer must return Dto (data transfer object) to your client and you can map dto to viewmodel .

Upvotes: 1

Sing
Sing

Reputation: 4052

First Question:

You should use partial class and metadata to seperate , just like below:

[MetadataType(typeof(StudentMD))]
public partial class Student
{
    public class StudentMD
   {
    public int ID { get; set; }
    [MaxLength(250)]
    [Required]
    public string LastName { get; set; }
    [Required]
    public string FirstMidName { get; set; }
    [Required]
    public DateTime EnrollmentDate { get; set; }
   }
}

Second Question:

It's OK to add a folder name "View Model"

I did it in my project too!

Upvotes: 0

Related Questions