Usage Environment: win7+vs2017
First, create a new MVC project for. NET Core2.0
Ii. using NuGet to add dependencies to EF
Input command: Install-package Microsoft.EntityFrameworkCore.SqlServer
Third,
if you are using DB First,
The model needs to be generated from the database
, you also need to add two dependencies using the command
Install-package Microsoft.EntityFrameworkCore.Tools
Install-package Microsoft.EntityFrameworkCore.SqlServer.Design
After the installation is successful, you can see it in the NuGet dependencies:
four.
based on a command, you can generate a model from a database
the
Pm> scaffold-dbcontext "server=.;D Atabase=task;integrated security=true; " Microsoft.entityframeworkcore.sqlserver-outputdir Models
Note: There is a problem when performing this step because the system is Win7,powershell version is too low, do not support this command, need to install more than 3.0 PowerShell version only line
1.Download from http://www.microsoft.com/en-us/download/details.aspx?id=34595
2. Install the 6.1 kernel version installation file (WINDOWS6.1-KB2506143-X64.MSU) for Windows Management Framework 3.0.
3. Restart
After adding success, you can see in models that the context object is generated and the model corresponding to the table
And now we can use EF.
1 Publiciactionresult Indexef2 ()2 {3Taskcontext TC =NewTaskcontext ();4 5 //list<userinfo> ulist = tc. Userinfo.tolist ();6 //var list = from P in TC. UserInfo select P;7 8 varList2 = TC. Userinfo.orderby (p = p.id). Where (p = p.id >Ten). Skip (Ten). Take (Ten). ToList ();9 Ten //viewbag.list = list; One //viewbag.ulist = ulist; A - returnView (list2); -}
Razor View page:
Using EF to access data in. NET Core2.0 MVC