# Quick Start on dotnet Core * [.NET 5.0 Download](https://dotnet.microsoft.com/download) and install visual studio code * alternative, install [visual studio Community](https://visualstudio.microsoft.com/vs/community/) * [Reference](https://docs.microsoft.com/en-us/dotnet/core/tools/) ```shell // list all templates $ 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 // run build assembly $ dotnet myapp.dll // build & run the current source project $ dotnet run // download all packages dependencies $ dotnet restore // $ 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 * Microsoft.EntityFrameworkCore * 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 ```