. 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: