Setup Entity Framework Core in ASP.NET Core
2017-07-21 16:04
706 查看
1. Run following command to install required packages:
Install-Package Microsoft.EntityFrameworkCore.SqlServer
2. For database first approach, need to run below two commands to install required tools:
Install-Package Microsoft.EntityFrameworkCore.Tools
Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design
3. Run following command to generate data models:
Scaffold-DbContext "Server=(local);Database=DBName;User ID=sa;Password=password;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Force -DataAnnotations -Context "CustomDBContext"
4. Move the connection string to config file:
1) Add connection string in file appsettings.json
2) Define static DBConnectionString property in DBContext file:
3) Get database connection string from appsettings.json file and assign it to DBConnectionString in Startup.cs file
参考链接:
https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/powershell https://joonasw.net/view/asp-net-core-1-configuration-deep-dive
Install-Package Microsoft.EntityFrameworkCore.SqlServer
2. For database first approach, need to run below two commands to install required tools:
Install-Package Microsoft.EntityFrameworkCore.Tools
Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design
3. Run following command to generate data models:
Scaffold-DbContext "Server=(local);Database=DBName;User ID=sa;Password=password;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Force -DataAnnotations -Context "CustomDBContext"
4. Move the connection string to config file:
1) Add connection string in file appsettings.json
{ "ConnectionStrings": { "CustomDBContext": "Server=(local);Database=DBName;User ID=sa;Password=password;" }, "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Warning" } }
2) Define static DBConnectionString property in DBContext file:
public partial class CustomDBContext : DbContext { ... public static string DBConnectionString { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(DBConnectionString); } ... }
3) Get database connection string from appsettings.json file and assign it to DBConnectionString in Startup.cs file
public void ConfigureServices(IServiceCollection services) { // Config database connection string NUHOTRSContext.DBConnectionString = Configuration.GetConnectionString("CustomDBContext"); ... }
参考链接:
https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/powershell https://joonasw.net/view/asp-net-core-1-configuration-deep-dive
相关文章推荐
- 使用ASP.NET Core MVC 和 Entity Framework Core 开发一个CRUD(增删改查)的应用程序
- 基于Asp.Net Core Mvc和EntityFramework Core 的实战入门教程系列-3
- [转]Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application (3 of 10)
- ASP.NET EntityFrameworkCore code first 多对多设计
- Asp.net Core中使用Entity Framework Core CodeFirst
- Setup Automapper in ASP.NET Core
- ASP.NET Core:使用EntityFrameworkCore操作MySql来丰富仓储模块
- ASP.NET Core 操作数据库提示“Microsoft.EntityFrameworkCore.DbUpdateException”
- 创建ASP.NET Core MVC应用程序(3)-基于Entity Framework Core(Code First)创建MySQL数据库表
- 使用ASP.NET Core MVC 和 Entity Framework Core 开发一个CRUD(增删改查)的应用程序
- ASP.NET CORE系列【六】Entity Framework Core 之数据迁移
- 使用 Visual Studio 的 ASP.NET Core MVC 和 Entity Framework Core 入门 (1 of 10)
- 基于Asp.Net Core Mvc和EntityFramework Core 的实战入门教程系列-4
- 基于Asp.Net Core Mvc和EntityFramework Core 的实战入门教程系列-5
- [转]ASP.NET Core 1.0: Using Entity Framework Core 1.0 - Transaction
- 创建ASP.NET Core MVC应用程序(3)-基于Entity Framework Core(Code First)创建MySQL数据库表
- asp.net Core Entity Framework 7 数据库更新维护
- 创建ASP.NET Core MVC应用程序(3)-基于Entity Framework Core(Code First)创建MySQL数据库表
- Asp .net core api+Entity Framework core 实现数据CRUD数据库中(附Git地址)
- 基于Asp.Net Core Mvc和EntityFramework Core 的实战入门教程系列-1