the model of the database development model takes precedence over the following steps :
1. Create an empty database in the database "No tables required"
If you do not create a new database in advance, if you do not select the database in the connection database, the default is to generate the table in the master database
2. Add the ADO Entity Data Model
Select the Models folder. Add--New item--Data--ado.net Entity Data Model--named "Modelfirst"--control model--complete
3. Modify the entity container name
Double-click MODELFIRST.EDMX, right-clicking on the blank page on the left-property---Modifying the entity container name is the name of the database connection string added in Web. config. can be taken at random "
4. Add an entity and modify the entity set name
modelfirst.edmx on the blank page right--new add--entity--Change the name of the entity set "name is the name of the table in the database"--OK
5. Add properties to the new table
Check Properties--right--new--scalar properties. This allows you to add the required attribute fields to the newly created table.
if necessary, right-click in the blank-"New-" Association-"Set your own desired association-" Determine to associate the relationship
6. Generate Table
6-1, in the white space--right---based on the model to generate the database---complete
6-2. On the SQL File page--right--connect--Connect,
6-3. On the SQL File page--right-click-Execute
7. View Results
7-1, open the database, in the previous set up a good database, already has a blank table
7-2. In the Web. config file, the node for the database connection string has been generated, and the node name is the entity container name that was previously set
7-3. The class corresponding to the table has been generated under the Xxx.tt file under the models file.
7-4. The data context class has been generated under the XXX.Context.tt file under the models file. The class name is the same as the entity container name
8. Create the controller and the corresponding view page
8-1. Create an instance of the data context class in the Controller
8-2. Operation data by Instance
The operation is as follows:
1. Create an empty database in the database "No tables required"
2. Add the ADO Entity Data Model
3. Modify the Entity container name
4. Add an entity and modify the entity set name
5. Add properties to the new table
6. Build database "is actually a table"
Note: In this step, if you did not create the database computer, and do not choose to connect to the database this option, test connection can also be connected successfully, then the resulting table is the default in the master database.
If this warning appears in the process, just click OK!
So far, the database has been built according to the model!
7. View Results
Database:
The Web. config configuration file automatically generates the nodes that connect to the database:
The class corresponding to the table has been generated under the Xxx.tt file under the models file.
The data context class has been generated under the XXX.Context.tt file under the models file. The class name is the same as the entity container name
8. Create the controller and the corresponding view page
creates an instance of the data context class in the controller, manipulating the data through the instance.
Controller class:
usingModelfirst.models;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;namespacemodelfirst.controllers{ Public classConputercontroller:controller {// //GET:/conputer/ PrivateModelfirstcomputer db =NewModelfirstcomputer (); PublicActionResult Index () {returnView (Db.ComputerInfo.ToList ()); } }}
View class:
@model ienumerable<modelfirst.models.computerinfo>@{Viewbag.title="Index";}@foreach (varIteminchModel) { <li> <strong> @Html. displaynamefor (model = model. Name):</strong>@Html. Displayfor (ModelItem= = Item. Name) <br/> <strong> @Html. displaynamefor (model = model. Price) </strong><br/>@Html. Displayfor (ModelItem=item. Price)</li> }</ul>
Results show:
The database development pattern of MVC Learning: Model First Instance Introduction