. NET core ASP. Web MVC Ef Site build vs2017 1)

Source: Internet
Author: User

1) Development environment construction

First download Install vs2017 address: https://www.visualstudio.com/zh-hans/downloads/

Installation tick a few items such as, note points in a single component when the. NET core runtime must be ticked, many people do not check the results of the new. NET Core project

2) Development

1. New. NET core MVC ASP.

2. New EF Project

First install three dependencies through NuGet

Install-package Microsoft.EntityFrameworkCore.SqlServer

Install-package Microsoft.EntityFrameworkCore.Tools

Install-package Microsoft.EntityFrameworkCore.Tools

3. Creating entity classes with NuGet commands

Scaffold-dbcontext "server= (localdb) \mssqllocaldb;database=blogging; Trusted_connection=true; " Microsoft.entityframeworkcore.sqlserver-outputdir Models

Create a finished file directory

Registering and configuring the context in Startup.cs

In order for the MVC controller to work BloggingContext , we will register it as a service.

    • Open Startup.cs
    • Add the following statement at the beginning of the file using
using EFGetStarted.AspNetCore.ExistingDb.Models;using Microsoft.EntityFrameworkCore;   
以及EF 上下文不要忘记添加构造函数

Now we can use the AddDbContext(...) method to register it as a service.

    • Find a ConfigureServices(...) method
    • Add the following code to register the context as a service
    
public void ConfigureServices(IServiceCollection services){ services.AddMvc(); var connection = @"Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;ConnectRetryCount=0"; services.AddDbContext<BloggingContext>(options => options.UseSqlServer(connection)); 
}

4.新增依赖于实类的控制器

. NET core ASP. Web MVC Ef Site build vs2017 1)

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.