0

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; }
    }

2 Answers 2

1

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 .

Sign up to request clarification or add additional context in comments.

Comments

0

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!

2 Comments

may i know what is the use of metadata annotations to separate? also, wouldn't it be duplicating if do in such way for code first(separate model from view model). Because in database first the class will be generated if database changes, but in code first we directly modify in the code
First Question:Because when you have modify your database, and you use code first from DB to modify the change. You will missing Some Datannotations like [DisplayName("fullname")]. Second: It wouldn't be duplicate if you use metadata annotation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.