Use asp.net core + ef core on vs2015, vs2015asp.net

Source: Internet
Author: User

Use asp.net core + ef core on vs2015, vs2015asp.net

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

Let's take a look at the implementation results.

 Before you start, make sure that the local machine has a. NET Core environment. Https://www.microsoft.com/net/core#windows

1. Create the file structure of the solution, for example, (the files that have been blurred are added by yourself and generated by ef ).

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

OpenProject. json,Microsoft. EntityFrameworkCore. SqlServer","Microsoft. EntityFrameworkCore. SqlServer. DesignAndMicrosoft. EntityFrameworkCore. Design"Add to"Dependencies";Microsoft. EntityFrameworkCore. Tools"Add to"Tools. (Note: Of course, all of these can be installed through NuGet. You must note that the"Microsoft. EntityFrameworkCore. Tools", Move it to"Tools)

3. OpenConnect to database, Set the database connection string

4. OpenPackage Management Console, And run the following command.-OutputDir ModelsPoint to the location where the generated file is storedModels

Scaffold-DbContext "Server = .; database = CoreDemo; Trusted_Connection = True; User ID = User connecting to the Database; Password = Password connecting to the Database; "Microsoft. entityFrameworkCore. sqlServer-OutputDir Models

 

5. OpenScaffold-DbContextGeneratedCoreDemoContext. csFile, removeOngrouping ingMethod

6. InCoreDemoContext. csAdd the following code

1 public CoreDemoContext(DbContextOptions<CoreDemoContext> option)2       : base(option)3 { }

7. OpenAppsettings. json, Add the following data connection configuration code

"ConnectionStrings": {"CoreDemoDatabase": "Server = .; database = CoreDemo; Trusted_Connection = True; User ID = User connecting to the Database; Password = Password connecting to the Database ;"}

8. OpenStartup. cs, ModifyConfigureServicesThe method is 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 UserController(CoreDemoContext context) 6         { 7             _context = context; 8         } 9 10         // GET: /<controller>/11         public IActionResult Index()12         {13             var users = (from u in _context.TUsers14                          select u).ToList();15             return View(users);16         }17 18         public IActionResult Create()19         {20             return View();21         }22 23         [HttpPost]24         [ValidateAntiForgeryToken]25         public async Task<IActionResult> Create(UserViewModel model)26         {27             if (!ModelState.IsValid)28             {29                 return View(model);30             }31 32             TUsers user = new TUsers()33             {34                 Name = model.Name35             };36             _context.TUsers.Add(user);37             int result = await _context.SaveChangesAsync();38             return RedirectToAction("Index");39         }40     }

10. CreateIndexView andCreateView

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, you can directly run the projectVs start, You can alsoCmdUse"Dotnet run"

 

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

Related Article

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.