Reference "ASP. MVC5 Advanced Programming" version 5th
4th Chapter Model
The model explored in this chapter is the object to display, save, create, update, and delete.
Scaffolding refers to the use of the tools provided by MVC to build, create, edit, and delete functions for each model object's standard index building, creating, editing, and deleting features. This build work is called scaffolding (scaffolding).
4.1 Modeling 4.2 Construction Scaffolding
The new ASP. NET MVC5 Project automatically contains a reference to the Entity class Framework (EF). EF is an object-relational mapping (object-relational mapping) framework. It knows how to save. NET objects in a relational database, and you can also use LINQ query statements to retrieve. NET objects that are saved in a relational database.
EF supports a development style with limited database, code-first and model-first, and the MVC scaffolding uses code-first style.
EF needs to know the time to modify the value of the model property, because it generates an UPDATE statement at this moment, making these changes consistent with the database.
4.2.2 Scaffolding and solid framework
1, Code precedence Conventions
EF also has conventions for foreign key relationships, database names, and so on. These conventions supersede all the mappings and configurations that previously needed to be provided to a relational object mapping framework.
2,dbcontext class
When using the code-first approach of EF, you need to use a class derived from the DbContext class of EF to access the database. The derived class has one or more Dbset <T> properties, and each T in the type represents an object that you want to persist.
4.2.3 Executing a scaffolding template
Loading related objects
- Preloading is the use of query statements to load all data.
- Lazy loading is loading only the primary object data.
4.2.4 Executing the Scaffolding code
1, create a database with the Entity Framework
You can control the database connection for the context in two ways.
- Preloading is the use of query statements to load all data.
- Lazy loading is loading only the primary object data.
_migrationhistory table
_migrationhistory stores a compressed version of the code-first model for each migration, allowing the database to be migrated across versions as needed.
2. Using the database initializer
When we use the Setinitializer method, we need to pass a Idatabaseinitializer object to it, and the framework comes with two Idatabaseinitializer objects: Dropcreatedatabasealways and Dropcreatedatabaseifmodelchanges.
3. Seeding the database
Calling the Seed method that overrides the base class saves the new object to the database.
Tips
To avoid adding duplicate data when you run the seed method multiple times on the same database. The dbset.addorupdate () extension method can be used in EF4.3 and later versions.
4.3 editing 4.3.1 Creating an edited resource
Edit View
When the user clicks the Submit button in the form, the HTML sends an HTTP POST request.
4.3.2 POST request when responding to edits
If the model is in a valid state, the Edit operation executes the following code: DB. Entry (album). state=entitystate.modified; This code tells the database context that the object already exists in the database.
If the model is invalid, or if the data is wrong, you need to return an error message to the user so that the user can correct the error.
4.4 Model Binding 4.4.1 Defaultmodelbinder
Over-posting attack: Change values that are not expected to be changed
4.4.2 an explicit model binding
Model bindings can be invoked explicitly using the Updatemodel and TryUpdateModel methods in the controller.
Reference "ASP. MVC5 Advanced Programming" version 5th
ASP. 4th Chapter MVC5