MVC is not a fire today, I am going to introduce a practical example of MVC5 and EF6 development, because the EF6 default is code first, so I will use the EF6 code first to make a simple example, in order to make the instance appear simple, here is a table To show the records of a table, and the model and the Dal are all in one project. Here are the detailed steps:
1. Create an MVC project open VS2013 New Project A Web project, the framework selected. NET Framewok4.5, the project name is Miniprofilerdemo. Such as:
Next, in the popup window, select the project template for MVC, such as:
2. Add Install EF Framework Dependency package to project
Select the project you just built, and right-click the following menu:
Click "Manage NuGet Packages" and click "Install" in the following screen entityframework 6.1
After the installation is successful, the associated DLL references are automatically added to the project.
3. Add a model to select the Models folder in the project and add a product class:
- Namespace Miniprofilerdemo. Models
- {
- public class Product
- {
- public int ID { get; set; }
- public string Name { get; set; }
- public decimal price { get; set; }
- public int Quantity { get; set; }
- }
- }
4. Add a context class for EF
Add an EF context class for the project to use as a public class for accessing the database:
- Using miniprofilerdemo. Models;
- Using System. Data. Entity;
- Namespace Miniprofilerdemo. DAL
- {
- public class efdbcontext:DbContext
- {
- public DbSet<Product> Products { get; set; }
- }
- }
To include a database link in Web. config:
- <connectionStrings>
- <add name="Efdbcontext" connectionString="server=.;D Atabase=miniprofilerdemo;uid=sa;pwd=sa; " providerName="System.Data.SqlClient" />
- </connectionStrings>
Note:The database link string above is based on your own database to make the appropriate adjustments, the database link node named "Efdbcontext" and the above-built EF's context class name. There is no node name specified in the EF context class, the default is that the class name is the same as the database link configuration node, and of course you can do it differently in practice, but this will add a constructor to your EF context class:
- Public Efdbcontext(): base("Database linked node name")
- {
- }
5. Create a controller and view that shows the model class
1. Select the Controller folder for the project and add a controller named product
- Using miniprofilerdemo. DAL;
- Using System. LINQ;
- Using System. Web. MVC;
- Namespace Miniprofilerdemo. Controllers
- {
- public class productcontroller : Controller
- {
- public actionresult Index()
- {
- Using (efdbcontext db=new efdbcontext())
- {
- var m = db. Products. ToList();
- return View(m);
- }
- }
- }
- }
2, move the cursor to the above action for the index method, right-click on the pop-up menu "Add View", enter the following content:
- @model List<miniprofilerdemo. Models. Product>
- @{
- ViewBag. Title = "ProductList";
- }
- productlist</h2>
- <table class= "Table" >
- <thead>
- <tr>
- <th>id</th>
- <th>Name</th>
- <th>price</th>
- <th>Quantity</th>
- </tr>
- </thead>
- <tbody>
- @foreach (var item in Model)
- {
- <tr>
- <td> @item. id</td>
- <td>@item. Name</td>
- <td> @item. price</td>
- <td>@item. Quantity</td>
- </tr>
- }
- </tbody>
- </table>
The model type of the binding for this view is strongly typed List<miniprofilerdemo.models.product>, and the data record is presented in a table.
6. View page, run results
The first run of the page, there is no data, this is normal, because the beginning of the database is not yet, when running EF will automatically create a database and model corresponding to the database link and EF context, such as:
Let's manually open the table product and add some records
Refresh the page again with the data you just added, such as:
MVC5 and EF6 Code First a complete example tutorial for getting Started