Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding ViewModel class adds codefirst model to database

I need a view-model in my ASP.NET MVC 5 project, but when I added one to the models folder, a new entity was added to the database and I was forced to add a migration and update my database. I do not want this to happen, as it is a view-model I am adding and not a model I need to persist back to the database. I want to scaffold some of the controller and views so I have added a primary key to the class. I did not add the newly created view-model to my DbContext class.

ViewModel:

    public class RolesViewModel 
    { 
        public int RolesViewModelId { get; set; }
        public string Role { get; set; } 
    }

Is there a way to create a view-model that doesn't automatically get added to the DbContext class, and therefore cause the data model to change?

Many thanks,

Jason.

like image 941
Jason James Avatar asked Mar 09 '14 22:03

Jason James


People also ask

Should ViewModel inherit from model?

So my question is, why it is wrong to inherit from a Model class? Your ViewModel should be separate from your Model (i.e. Entity in this context). Remember a "ViewModel" is a model for the view. In other words it can display same, compact or transformed information of the original model.

What is the difference between model and ViewModel?

A model is usually more closely related to how your data is stored (database, services, etc.) and the model will closely resemble those. The ViewModel on the other hand is closely related to how your data is presented to the user. It is usually a flatten version of your model, denormalized, etc.

How do I add a model to Razor view?

Right-click on the Pages/Movies folder > Add > New Scaffolded Item. In the Add New Scaffold dialog, select Razor Pages using Entity Framework (CRUD) > Add. Complete the Add Razor Pages using Entity Framework (CRUD) dialog: In the Model class drop down, select Movie (RazorPagesMovie.

What should you do to scaffold database to a model part of MVC?

Right-click the Controllers folder, and select Add > New Scaffolded Item. Select the MVC 5 Controller with views, using Entity Framework option. This option will generate the controller and views for updating, deleting, creating and displaying the data in your model.


2 Answers

Whether you call it a view model, an entity, etc. it's just semantics. Everything is just a class, and the context it's used in determines what you refer to it as. In the case of entities, that's adding a reference either explicitly or implicitly in your DbContext, and that's the only way you'll end up with something added to your database. I emphasized the "or implicitly* part because if any class that is referenced in the your DbContext, or any class connected to any class referenced there, also references your "view model", it will end up in your database. Entity Framework will automatically follow your class hierarchies and create tables for all relationships, even if you do not reference a particular class in those hierarchies directly in your DbContext.

like image 83
Chris Pratt Avatar answered Oct 06 '22 04:10

Chris Pratt


In your case Scaffolding will add the following code below in your appContext Class

public DbSet<RolesViewModel> RolesViewModel { get; set; }

You can still use scaffolding if you wish, however remember to remove that entry and no table will be created by code first. It will keep your database clean.

like image 22
Adrian Hedley Avatar answered Oct 06 '22 05:10

Adrian Hedley