You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
4 years ago | |
|---|---|---|
| .. | ||
| README.md | 4 years ago | |
README.md
Quick Start on dotnet Core
- .NET 5.0 Download and install visual studio code
- alternative, install visual studio Community
- Reference
// 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 <packagename>
// 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 (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
// 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
- Packages to add
- Microsoft.EntityFrameworkCore
- Pomelo.EntityFrameworkCore.MySql
- Microsoft.EntityFrameworkCore.Tools
$ dotnet add package Microsoft.EntityFrameworkCore
$ dotnet add package Pomelo.EntityFrameworkCore.MySql
$ dotnet add package Microsoft.EntityFrameworkCore.Tools
- Models
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<Pet> Pets { get; set; }
}
}
- DBContext
using Microsoft.EntityFrameworkCore;
using TempWeb.Models;
namespace TempWeb.Data
{
public class DataContext : DbContext
{
public DbSet<Pet> Pets { get; set; }
public DbSet<User> Users { get; set; }
public DataContext(DbContextOptions<DataContext> options)
: base(options) { }
}
}
- appsettings.json
...
"ConnectionStrings": {
"DefaultConnection": "server=localhost;port=3306;database=test;user=test_user;password=test"
}
...
- startup
...
using TempWeb.Data;
using Microsoft.EntityFrameworkCore;
using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
...
public void ConfigureServices(IServiceCollection services)
{
string mySqlConnectionStr = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContextPool<DataContext>(
options => options.UseMySql(
mySqlConnectionStr, ServerVersion.AutoDetect(mySqlConnectionStr)
));
services.AddMvc();
}
- Controller with injected context
public class PetsController : ControllerBase
{
private readonly DataContext _context;
public PetsController(DataContext context)
{
_context = context;
}
}
- CLI
$ dotnet ef migrations add DbInit
$ dotnet ef database update
- Alternative, using
Package Manager Console(Visual Studio)
PM> Add-Migration DbInit
PM> Update-Database