使用环境:Win7+VS2017
输入命令:Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install-Package Microsoft.EntityFrameworkCore.Tools
Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design
安装成功后就可以在Nuget依赖项中看到:
PM> Scaffold-DbContext "Server=.;Database=Task;Integrated Security=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
注:执行这一步的时候出现了点问题 ,因为系统是win7,powershell版本太低了,不支持这个命令,需要安装3.0以上的powershell版本才行
1.Download from http://www.microsoft.com/en-us/download/details.aspx?id=34595
2. 安装Windows Management Framework 3.0的6.1内核版本安装文件(Windows6.1-KB2506143-x64.msu)。
3.重启
添加成功后在models可以看到, 生成了上下文对象与和表对应的model
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视图页: