MVC5 and EF6 Code First a complete example tutorial for getting Started

Source: Internet
Author: User
Tags connectionstrings

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:

  1. Namespace Miniprofilerdemo. Models
  2. {
  3. public class Product
  4. {
  5. public int ID { get; set; }
  6. public string Name { get; set; }
  7. public decimal price { get; set; }
  8. public int Quantity { get; set; }
  9. }
  10. }

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:

  1. Using miniprofilerdemo. Models;
  2. Using System. Data. Entity;
  3. Namespace Miniprofilerdemo. DAL
  4. {
  5. public class efdbcontext:DbContext
  6. {
  7. public DbSet<Product> Products { get; set; }
  8. }
  9. }

To include a database link in Web. config:
    1. <connectionStrings>
    2. <add name="Efdbcontext" connectionString="server=.;D Atabase=miniprofilerdemo;uid=sa;pwd=sa; " providerName="System.Data.SqlClient" />
    3. </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:
    1. Public Efdbcontext(): base("Database linked node name")
    2. {
    3. }

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
  1. Using miniprofilerdemo. DAL;
  2. Using System. LINQ;
  3. Using System. Web. MVC;
  4. Namespace Miniprofilerdemo. Controllers
  5. {
  6. public class productcontroller : Controller
  7. {
  8. public actionresult Index()
  9. {
  10. Using (efdbcontext db=new efdbcontext())
  11. {
  12. var m = db. Products. ToList();
  13. return View(m);
  14. }
  15. }
  16. }
  17. }
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:
  1. @model List<miniprofilerdemo. Models. Product>
  2. @{
  3. ViewBag. Title = "ProductList";
  4. }
  5. productlist</h2>
  6. <table class= "Table" >
  7. <thead>
  8. <tr>
  9. <th>id</th>
  10. <th>Name</th>
  11. <th>price</th>
  12. <th>Quantity</th>
  13. </tr>
  14. </thead>
  15. <tbody>
  16. @foreach (var item in Model)
  17. {
  18. <tr>
  19. <td> @item. id</td>
  20. <td>@item. Name</td>
  21. <td> @item. price</td>
  22. <td>@item. Quantity</td>
  23. </tr>
  24. }
  25. </tbody>
  26. </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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.