. NET Core2.0 MVC uses EF to access data, core2.0mvc

Source: Internet
Author: User

. NET Core2.0 MVC uses EF to access data, core2.0mvc

Environment: Win7 + VS2017

1. Create a. NET Core2.0 MVC Project

 

 

 

Ii. Use Nuget to add EF Dependencies

Enter the command: Install-Package Microsoft. EntityFrameworkCore. SqlServer

 

III, If you are using db first, Generate a model based on the database To add two dependencies

Install-Package Microsoft. EntityFrameworkCore. Tools

Install-Package Microsoft. EntityFrameworkCore. SqlServer. Design

 

After the installation is successful, you can see in the Nuget dependency:

 

4. You can generate a model from the Database Based on a command. Now

PM> Scaffold-DbContext "Server =.; Database = Task; Integrated Security = True;" Microsoft. EntityFrameworkCore. SqlServer-OutputDir Models

Note: Some problems occurred during the execution of this step, because the system is win7, The powershell version is too low, and this command is not supported, you need to install the powershell version above 3.0.

1. Download from http://www.microsoft.com/en-us/download/details.aspx? Id = 34595

2. Install the Windows Management Framework 3.0 6.1 kernel Installation File (Windows6.1-KB2506143-x64.msu ).

3. Restart

 

After successful addition, you can see in models that the context object and the model corresponding to the table are generated.

 

5. Now you can use EF.
 1 public IActionResult Indexef2() 2         { 3             TaskContext tc = new TaskContext(); 4  5             //List<UserInfo> ulist = tc.UserInfo.ToList(); 6             //var list = from p in tc.UserInfo select p; 7  8             var list2 = tc.UserInfo.OrderBy(p => p.Id).Where(p => p.Id > 10).Skip(10).Take(10).ToList(); 9 10             //ViewBag.list = list;11             //ViewBag.ulist = ulist;12 13             return View(list2);14         }

Razor view page:

 

Related Article

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.