From 4ee290c12308b6aa886abaebc4833638f03a6c04 Mon Sep 17 00:00:00 2001 From: Yik Teng Hie Date: Tue, 8 Jun 2021 14:56:49 +0800 Subject: [PATCH] ef core quick guide --- dotnet/README.md | 182 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 181 insertions(+), 1 deletion(-) diff --git a/dotnet/README.md b/dotnet/README.md index ab43640..7d56405 100644 --- a/dotnet/README.md +++ b/dotnet/README.md @@ -12,18 +12,41 @@ $ dotnet new --list // scaffold a template project $ dotnet new --install Microsoft.DotNet.Web.Spa.ProjectTemplates +// create REST Api project +$ dotnet new webapi + +// list installed SDK +$ dotnet --list-sdks + +// create global.json with configuration for SDK 3.1.101 +// the config is used by .NET CLI to decide which SDK to build on +// By default, without this file, .NET CLI will use the latest SDK installed +$ dotnet new globaljson --sdk-version 3.1.101 + // clean output of project $ dotnet clean // build the project $ dotnet build -// build & run the project +// run build assembly +$ dotnet myapp.dll + +// build & run the current source project $ dotnet run // $ dotnet add package + +// create solution group of projects +$ dotnet new sln -n mysolution +$ dotnet new console -o myapp +$ dotnet new classlib -o mylib1 +$ dotnet new classlib -o mylib2 +$ dotnet sln mysolution.sln add myapp\myapp.csproj +$ dotnet sln mysolution.sln add mylib1\mylib1.csproj --solution-folder mylibs +$ dotnet sln mysolution.sln add mylib2\mylib2.csproj --solution-folder mylibs ``` * [Entity Framework Core](https://docs.microsoft.com/en-us/ef/) (EF Core) for SQL @@ -31,15 +54,172 @@ $ dotnet add package * Microsoft.EntityFrameworkCore.Relational * Microsoft.EntityFrameworkCore.SqlServer * Microsoft.EntityFrameworkCore.Tools + * Pomelo.EntityFrameworkCore.MySql + * MySql.Data.EntityFrameworkCore (do not support ef migration) * List of [EF Core Providers](https://docs.microsoft.com/en-us/ef/core/providers/?tabs=dotnet-core-cli) ```shell +// install ef core tool +$ dotnet tool install --global dotnet-ef + +// update ef core tool +$ dotnet tool update --global dotnet-ef +/////////////////////////////// +// Relaunch command prompt to effect the installation +///////////////////////////// + +// add ef core design package to a project +$ dotnet add package Microsoft.EntityFrameworkCore.Design + +// verify ef core installed $ dotnet ef +// manage database $ dotnet ef database + +// manage DBContext Type $ dotnet ef dbcontext +// manage migrations $ dotnet ef migrations + +// Steps +// 1. Create models +// 2. Create the DBContext class +// 3. Configuration appsetting.json +// 4. configure service at starup class +// +// example cli execution +$ dotnet ef migrations add DbInit +$ dotnet ef database update +``` + +## EF Core Migration Quick Guide + +* [Reference](https://medium.com/@stas_khavruk/how-to-properly-use-ef-core-in-asp-net-core-with-mysql-database-a75f56c97318) +* Packages to add + * *Microsoft.EntityFrameworkCore* + * *Pomelo.EntityFrameworkCore.MySql* + * *Microsoft.EntityFrameworkCore.Tools* + +```sh +$ dotnet add package Microsoft.EntityFrameworkCore +$ dotnet add package Pomelo.EntityFrameworkCore.MySql +$ dotnet add package Microsoft.EntityFrameworkCore.Tools +``` + + + +* Models + +```C# +namespace TempWeb.Models +{ + public class Pet + { + public int Id { get; set; } + public string Name { get; set; } + } +} + +using System.Collections.Generic; + +namespace TempWeb.Models +{ + public class User + { + public int Id { get; set; } + public string FirstName { get; set; } + public string LastName { get; set; } + + public IList Pets { get; set; } + } +} +``` + + + +* DBContext + +```C# +using Microsoft.EntityFrameworkCore; +using TempWeb.Models; + +namespace TempWeb.Data +{ + public class DataContext : DbContext + { + public DbSet Pets { get; set; } + public DbSet Users { get; set; } + + public DataContext(DbContextOptions options) + : base(options) { } + } +} +``` + + + +* appsettings.json + +```json +... +"ConnectionStrings": { + "DefaultConnection": "server=localhost;port=3306;database=test;user=test_user;password=test" + } +... +``` + + + +* startup + +```C# +... +using TempWeb.Data; +using Microsoft.EntityFrameworkCore; +using Pomelo.EntityFrameworkCore.MySql.Infrastructure; +... +public void ConfigureServices(IServiceCollection services) +{ + string mySqlConnectionStr = Configuration.GetConnectionString("DefaultConnection"); + services.AddDbContextPool( + options => options.UseMySql( + mySqlConnectionStr, ServerVersion.AutoDetect(mySqlConnectionStr) + + )); + services.AddMvc(); +} +``` + +* Controller with injected context + +```C# +public class PetsController : ControllerBase +{ + private readonly DataContext _context; + + public PetsController(DataContext context) + { + _context = context; + } +} +``` + + + +* CLI + +```sh +$ dotnet ef migrations add DbInit +$ dotnet ef database update +``` + +* Alternative, using `Package Manager Console` (Visual Studio) + +```sh +PM> Add-Migration DbInit +PM> Update-Database ```