asp.net-mvclinqef-database-first

Modified model that generated from database first approach


Beginner here

Assume that I have a model generated from the database first.

I want to manipulate that model without doing anything to that model.

How can I do that?

I thought about creating another model that related to the model generated from the database first but it is just an idea, I don't know how to do it

I would very much appreciate an answer with an example


Solution

  • Try something like this

    //Get what uou need from the data
    var result = context.Student
        .Where(x => x.Name) //Query the Db for what you need
        .Select(x => new
        {
            Birthday = x.Birthday.ToString("MM/dd/yyyy"), //Use format you need
            Name = x.Name
            //Add props you need from student
        }).ToList();
    

    You can see some DateTiME FORMATS HERE https://www.c-sharpcorner.com/blogs/date-and-time-format-in-c-sharp-programming1

    Or if you want to use that model you can add to it ModifiedBirdday without affected the database like

    public class Student
    {
        //All student props
    
    
        [NotMapped]
        public string ModifiedBirthday => this.Birthday.Value.ToString("MM/dd/yyyy"); // Format you need
    
    }