Using EF to access data in. NET Core2.0 MVC

Source: Internet
Author: User

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

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.