Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I specify a data context class when scaffolding a view?

When adding a new view to an ASP.NET MVC 5 project using the Add View dialog pictured below, I'm invited to choose a template and a model class, which allows me to quickly generate a form for creating new instances of the model or a view that displays the model's properties. But why should the view care what the data context class is? In my project, whether or not I specify the data context class, the same view is generated, but I'm guessing there's a scenario where it would make a difference. What might that be?

ASP.NET MVC

like image 242
regularmike Avatar asked Jun 21 '14 15:06

regularmike


People also ask

What does scaffolding mean in terms of ASP NET core projects What is an example of its utility?

Scaffolding in ASP.NET Core is a technique used to generate code at design time to support a number of common application scenarios when working with Entity Framework Core. The code generation tool is available as a Nuget package.

What is @model in Cshtml?

The @model directive allows access to the list of movies that the controller passed to the view by using a Model object that's strongly typed. For example, in the Index.cshtml view, the code loops through the movies with a foreach statement over the strongly typed Model object: CSHTML Copy.

What is meant by scaffolding in MVC?

Scaffolding is used to define the code-generation framework used in web applications. It uses T4 templates to generate basic controllers and views for the models. It generates instances for the mapped domain model and code for all CRUD operations.


1 Answers

If you refer to an existing DbContext then the wizard will insert public DbSet<Employee> Employee { get; set; } (if it doesn't already exist) in your DbContext derived class . Looks like Visual Studio doing some of the leg work.

like image 186
Andrew Avatar answered Sep 19 '22 08:09

Andrew