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)