ASP. Core+ef Core

Source: Internet
Author: User

ASP. Core+ef Core

Official Document Https://docs.asp.net/en/latest/tutorials/first-mvc-app/start-mvc.html

Let's look at the effect of the implementation

before you begin, make sure that the. NET core environment is already in place. Https://www.microsoft.com/net/core#windows

1. Create the file structure of the solution as (obfuscated over files are added later and EF generated).

2. To use EF Core, first reference the EF core-related packages. Https://docs.efproject.net/en/latest/platforms/aspnetcore/existing-db.html

Open Project.json, "Microsoft.EntityFrameworkCore.SqlServer", " Microsoft.EntityFrameworkCore.SqlServer.Design"and"Microsoft.EntityFrameworkCore.Design"are added to the" Dependencies, add "Microsoft.EntityFrameworkCore.Tools" to "Tools". (Note: Of course, these can all be installed through NuGet, note that when installing "Microsoft.EntityFrameworkCore.Tools" with NuGet, you need to move it toTools)

3. Open Connect to database , set database connection string

4. Open the package Management Console and run the following command. -outputdir Models points to the file where the makefile is stored Models

Scaffold-dbcontext "server=.;D Atabase=coredemo; Trusted_connection=true; User id= your connection to the database; password= the password for your connection database; " Microsoft.entityframeworkcore.sqlserver-outputdir Models

5. Open the CoreDemoContext.cs file that you just created with scaffold-dbcontext , remove the onconfiguring method

6. Add the following code to the CoreDemoContext.cs

1 Public coredemocontext (dbcontextoptions<coredemocontext> option) 2       : Base (option) 3 {}

7. Open Appsettings.json, add the following data connection Configuration code

"ConnectionStrings": {    "coredemodatabase": "Server=.;D Atabase=coredemo; Trusted_connection=true; User id= your connection to the database; password= the password for your connection database; "  }

8. Open Startup.cs, modify the configureservices method as follows

1 public void configureservices (Iservicecollection services) 2 {3       //ADD framework Services.4       Services. Addmvc (); 5       services. adddbcontext<coredemocontext> (option = option. Usesqlserver (configuration.getconnectionstring ("coredemodatabase")); Get SQL Connection Configuration 6}

9. Add a new controller--Usercontroller

 1 public class Usercontroller:controller 2 {3 private coredemocontext _context; 4 5 public Us Ercontroller (Coredemocontext context) 6 {7 _context = context; 8} 9//GET:/< CONTROLLER&GT;/11 public Iactionresult Index () () {"var" users = (from u in _context. TUSERS14 select u).             ToList ();}17 public Iactionresult Create () 19 {20 return View ();}22 [httppost]24 [validateantiforgerytoken]25 Public Async task< Iactionresult> Create (Userviewmodel model) Modelstate.isvalid) (model),}31 tusers user = n EW tusers (). {"Name = Model". Name35};36 _context. Tusers.add (user), PNs int result = Await _Context. Savechangesasync (); Redirecttoaction return ("Index"); 39}40}

10. Creating the Index view and the CREATE view

1 @model ienumerable<tusers> 2  3 @{4     viewbag.title = "user list"; 5} 6  7 
 1 @model WebDemo.Models.UserViewModel 2 3 @{4 viewbag.title = "New user"; 5} 6 7 

11. In this step, the running project can be started directly with vs , or you can use "dotnet run" in cmd

Reprint please mark the original address: http://www.cnblogs.com/JasonLong/p/5653273.html

ASP. Core+ef Core

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.